ST_PointN
What is ST_PointN?
ST_PointN is a PostGIS function that returns the Nth point of a LINESTRING or CIRCULARSTRING as a POINT geometry. Index is 1-based; negative indices count from the end (-1 is the last point).
ST_PointN(geometry line, integer n) → geometryOut-of-range indices return NULL. Non-line input returns NULL.
When would you use ST_PointN?
Use ST_PointN to access a specific vertex of a line by position — typical cases include pulling the first/last points, picking a midpoint vertex for sampling, or comparing vertices with neighbours:
1SELECT id,
2 ST_PointN(geom, 1) AS first_vertex,
3 ST_PointN(geom, -1) AS last_vertex,
4 ST_PointN(geom, (ST_NumPoints(geom) / 2) + 1) AS middle_vertex
5FROM routes;For the start/end specifically, use ST_StartPoint and ST_EndPoint — they are clearer. Use ST_PointN for arbitrary middle indices.
FAQs
Does ST_PointN support negative indices?
Yes. Negative indices count from the end — ST_PointN(line, -1) is the last point, -2 the second-to-last, and so on.
What does it return for MultiLineString?
NULL. Unpack with ST_GeometryN first to select a component.
How is it different from ST_DumpPoints?
ST_PointN returns a single point by index. ST_DumpPoints returns every vertex as a row. Use ST_PointN for targeted access, ST_DumpPoints for iteration.
Are Z and M preserved?
Yes. The returned point has the same dimensionality as the source vertex.