ST_Azimuth
What is ST_Azimuth?
ST_Azimuth returns the north-based azimuth — the compass bearing in radians — of the straight line from one point to another. The value is measured clockwise from north, ranges from 0 to 2π, and is NULL when the two points are identical.
1ST_Azimuth(geometry pointA, geometry pointB) → float
2ST_Azimuth(geography pointA, geography pointB) → floatFor geometry, the azimuth is computed in the plane using the points' coordinates. For geography, the azimuth is the initial bearing on the great-circle (or spheroidal) path between the two points.
When would you use ST_Azimuth?
Use ST_Azimuth whenever you need the direction between two points: labelling roads with their bearing, rotating map icons so an arrow points toward a destination, computing approach headings for aviation or maritime routing, or classifying wind/flow vectors stored as from/to point pairs. It is also useful inside ST_Project to offset a point a given distance in a given direction.
Because the return value is in radians, wrap it with degrees() for human-readable output:
1SELECT id, degrees(ST_Azimuth(start_pt, end_pt)) AS bearing_deg
2FROM routes;FAQs
What units is the return value in?
Radians, measured clockwise from north (0 = north, π/2 = east, π = south, 3π/2 = west). Convert to compass degrees with PostgreSQL's built-in degrees() function.
What happens if the two points are equal?
ST_Azimuth returns NULL when the two input points have identical coordinates, because a bearing is undefined. Guard your calls accordingly if you might pass coincident points.
Does ST_Azimuth work on geography?
Yes. On geography it returns the initial great-circle bearing from point A to point B, computed on the spheroid. The bearing at the destination differs from the initial bearing on long paths because meridians converge — this is the normal behaviour on an oblate earth.
Can I use ST_Azimuth on anything other than points?
No. Both arguments must be POINT geometries (or POINT geographies). For non-point inputs, extract a representative point first with ST_Centroid, ST_PointOnSurface, or ST_StartPoint/ST_EndPoint for lines.