0Pricing
SQL Academy · Lesson

Geospatial Indexing with PostGIS

Add the PostGIS extension, store geometry/geography, and query spatial relationships with GiST indexes.

Geospatial Indexing with PostGIS is a free SQL Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is PostGIS?

The most powerful geospatial extension in any open source DB. Adds geometry/geography types, hundreds of spatial functions, and GiST/SP-GiST indexes for fast spatial queries.

Enable PostGIS

It's a contrib extension:

CREATE EXTENSION postgis;

SELECT PostGIS_Version();

GEOMETRY vs GEOGRAPHY

  • GEOMETRY — planar, fast, units of the coordinate system (often meters / degrees)
  • GEOGRAPHY — spherical earth, slower, units always in meters

Pick GEOGRAPHY for "distances on earth" with mixed-region data; GEOMETRY for projected coordinate systems (regional GIS, fast operations).

Storing Points

Common pattern: store a lat/lon as a GEOGRAPHY point:

CREATE TABLE places (
  id BIGSERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  location GEOGRAPHY(POINT, 4326)
);

INSERT INTO places (name, location)
VALUES ('Berlin', ST_GeogFromText('SRID=4326;POINT(13.405 52.520)'));

SRID

The "Spatial Reference ID" defines the coordinate system. WGS84 (id 4326) is standard for GPS lat/lon.

GiST Spatial Index

Index your geometry column with GiST:

CREATE INDEX places_location_idx ON places USING GIST (location);
-- Now spatial filters are fast.

Nearest Neighbour: <->

The <-> KNN operator with an index returns nearest results in milliseconds:

SELECT name, ST_Distance(location, ST_GeogFromText('SRID=4326;POINT(13.4 52.5)')) AS m
FROM places
ORDER BY location <-> ST_GeogFromText('SRID=4326;POINT(13.4 52.5)')
LIMIT 10;

Within Distance

Find places within 2km of a point:

SELECT name FROM places
WHERE ST_DWithin(
  location,
  ST_GeogFromText('SRID=4326;POINT(13.4 52.5)'),
  2000  -- meters
);

Polygons and Areas

Store and query polygons (city boundaries, delivery zones):

CREATE TABLE zones (
  id BIGSERIAL PRIMARY KEY,
  name TEXT,
  area GEOGRAPHY(POLYGON, 4326)
);

-- Find places inside a zone:
SELECT p.name FROM places p
JOIN zones z ON ST_Within(p.location::GEOMETRY, z.area::GEOMETRY)
WHERE z.name = 'Mitte';

Intersect, Contains, Within

Rich set of spatial predicates: ST_Intersects, ST_Contains, ST_Within, ST_Touches, ST_Disjoint, ST_Equals.

Loading GeoJSON

Import GeoJSON straight into PostGIS:

INSERT INTO zones (name, area)
SELECT 'Mitte', ST_GeogFromGeoJSON('{"type":"Polygon","coordinates":[...]}');

Vector Tile Generation

PostGIS 3.0+ can generate Mapbox vector tiles directly with ST_AsMVT — power map UIs straight from the DB.

Recap

PostGIS turns Postgres into a serious GIS engine.

  • GEOMETRY for projected, GEOGRAPHY for earth-distance
  • GiST index for spatial filters and KNN
  • ST_DWithin, ST_Distance, ST_Within
  • SRID 4326 = WGS84 lat/lon

Quick Check

You store user locations as GEOGRAPHY(POINT). Which index type accelerates "find within 5km" queries?

Frequently asked questions

Is the “Geospatial Indexing with PostGIS” lesson free?

Yes — the full text of “Geospatial Indexing with PostGIS” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Geospatial Indexing with PostGIS”?

Add the PostGIS extension, store geometry/geography, and query spatial relationships with GiST indexes. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Geospatial Indexing with PostGIS” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Trigram Search (pg_trgm) for Fuzzy Matching
  2. Full-Text Search with tsvector and GIN
  3. Geospatial Indexing with PostGIS
  4. Vector Search with pgvector
← Back to SQL Academy