QA
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→

More PostgreSQL Interview Questions

Beginner LevelPostgreSQL

How to use pgvector in PostgreSQL for vector similarity search with HNSW indexes?

Beginner LevelPostgreSQL

What is PostgreSQL architecture and how do client backends connect via process fork model?

Beginner LevelPostgreSQL

What special built-in data types exist in PostgreSQL (JSONB, UUID, ARRAY, INET, TEXT)?