Intermediate LevelPostgreSQL
How do cube and earthdistance extensions calculate latitude/longitude distances?
❓ Interview Prompt: Explain `earthdistance` and `cube` extensions, computing spherical distance between lat/lng points, and indexing earth coordinates without PostGIS.
For applications needing simple geographic point distance calculations without installing the full PostGIS suite, the 'earthdistance' extension computes distance over a 3D Earth cube model:
Code Implementation Example
sql
CREATE EXTENSION IF NOT EXISTS cube;
CREATE EXTENSION IF NOT EXISTS earthdistance;
-- Calculate distance in meters between NYC and Boston coordinates
SELECT earth_distance(
ll_to_earth(40.7128, -74.0060),
ll_to_earth(42.3601, -71.0589)
) AS distance_meters;💡Calculating geographic point distance using earthdistance extension.
🎯 Interview Key Takeaway
Use earthdistance for simple point-to-point geographic calculations when PostGIS is not installed.
#earthdistance#cube#Geographic Distance#Intermediate
Back to all PostgreSQL Questions→