ST_X
What is ST_X?
ST_X is a PostGIS function that returns the X coordinate of a POINT geometry. For lat/lon data (EPSG:4326), X is longitude.
ST_X(geometry point) → double precisionInput must be a single POINT. For other types, extract a specific vertex first with ST_StartPoint, ST_EndPoint, ST_PointN, or ST_Centroid.
When would you use ST_X?
Use ST_X (paired with ST_Y) to extract numeric coordinates for export, display, or downstream computation — the canonical pattern for producing longitude/latitude CSVs or API responses:
1SELECT id,
2 ST_X(geom) AS lon,
3 ST_Y(geom) AS lat
4FROM sites
5WHERE GeometryType(geom) = 'POINT';Also common for distance-free comparisons in simple cases (e.g. "rows east of this longitude") and for feeding coordinates back into ST_MakePoint during geometry-editing workflows.
FAQs
Is ST_X longitude or latitude for EPSG:4326?
Longitude. PostGIS follows X = east-west, Y = north-south. Swapping them is the single most common PostGIS bug.
What does ST_X return for non-Point input?
NULL. Extract a specific vertex first (e.g. via ST_Centroid for polygons).
How do I get X for every vertex of a line?
SELECT ST_X((ST_DumpPoints(geom)).geom) FROM t; — expand vertices with ST_DumpPoints first.
Does ST_X consider Z or M?
No — it returns only the X component. Use ST_Y, ST_Z, ST_M for the other components.