Skip to main content

ST_Distance

Calculates the minimum Cartesian distance between two geometries.

Syntax

ST_Distance(geometry_a, geometry_b)

Parameters

ParameterTypeDescription
geometry_aGeometryFirst geometry
geometry_bGeometrySecond geometry

Returns

TypeDescription
Float64Minimum distance between the two geometries

Examples

Distance Between Two Points

SELECT ST_Distance(
ST_Point(0, 0),
ST_Point(3, 4)
);
-- Returns: 5.0

Distance from Point to Polygon

SELECT ST_Distance(
ST_Point(5, 5),
ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))')
);
-- Returns: ~1.414 (diagonal distance to nearest corner)

Find Nearby Features

SELECT id, name, ST_Distance(geom, ST_Point(-122.4, 37.8)) as distance
FROM landmarks
ORDER BY distance
LIMIT 10;

Filter by Distance

SELECT * FROM buildings
WHERE ST_Distance(geom, ST_Point(-122.4194, 37.7749)) < 1000;

Notes

  • Returns 0 when geometries touch or overlap
  • Distance is in the same units as the input geometry coordinates
  • For geographic coordinates (lat/lon), returns distance in degrees (not meters)
  • For meter-based calculations with geographic data, consider projecting to an appropriate local CRS first

See Also