The earthdistance extension in PostgreSQL provides functions for calculating great-circle distances between points on Earth. It is useful for applications that require geospatial distance calculations, such as location-based services and mapping applications. Your Nile database arrives withDocumentation Index
Fetch the complete documentation index at: https://thenile.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
earthdistance extension and its dependency cube already enabled, so there’s no need to run create extension.
Creating and Populating locations Table
Before performing distance calculations, let’s create a sample table to store latitude and longitude values:
Calculating Distance Between Two Points
To compute the great-circle distance between two points (given in latitude and longitude in degrees), use theearth_distance() function:
Finding Locations Within a Given Radius
To find all locations within 1000 km of San Francisco:Limitations
earthdistanceassumes a spherical Earth model, which may introduce minor inaccuracies.- Distance calculations are approximate and may not be suitable for high-precision geospatial applications.
- Requires both
cubeandearthdistanceextensions to be installed.
Removing an Index
If you need to remove the spatial index:Conclusion
Theearthdistance extension in PostgreSQL simplifies great-circle distance calculations for geographic coordinates. It is useful for applications needing fast location-based searches and distance queries.
For more details, refer to the PostgreSQL documentation.