postgis postgresql
David Sterling  

From CREATE EXTENSION postgis to First Map: A 15-Minute Quickstart

If you’ve ever wanted to add spatial capabilities to your PostgreSQL database but weren’t sure where to start, this guide is for you. In just 15 minutes, you’ll go from enabling the PostGIS extension to visualizing your first geographic data on a map.

What is PostGIS?

PostGIS is a powerful open-source extension that transforms PostgreSQL into a full-featured spatial database. It adds support for geographic objects, allowing you to store, query, and manipulate location data directly in your database. Think points on a map, routes between locations, or entire geographic regions—all queryable with SQL.

Prerequisites

Before we begin, make sure you have:

  • PostgreSQL 12 or later installed
  • Admin access to your database
  • A SQL client (psql, pgAdmin, or any client of your choice)
  • 15 minutes of focused time

Step 1: Enable the PostGIS Extension

First, connect to your PostgreSQL database and run this simple command:

CREATE EXTENSION postgis;

That’s it! Your database now has spa

Step 2: Create a Table with Spatial Data

Let’s create a simple table to store locations of coffee shops:

CREATE TABLE coffee_shops (
id SERIAL PRIMARY KEY,
name TEXT,
location GEOGRAPHY(POINT, 4326)
);

The GEOGRAPHY type is perfect for real-world coordinates. The 4326 refers to the WGS 84 coordinate system—the same one used by GPS.

Step 3: Insert Some Location Data

Now let’s add a few coffee shops with their coordinates:

INSERT INTO coffee_shops (name, location) VALUES
(‘Blue Bottle Coffee’, ST_Point(-122.4194, 37.7749)),
(‘Philz Coffee’, ST_Point(-122.4083, 37.7849)),
(‘Sightglass Coffee’, ST_Point(-122.4075, 37.7699));

ST_Point creates a geographic point from longitude and latitude values. Notice the order: longitude first, then latitude!

Step 4: Query Your Spatial Data

Now for the fun part—let’s find coffee shops within 1 kilometer of a specific location:

SELECT name,
ST_Distance(location, ST_Point(-122.4194, 37.7749)) as distance_meters
FROM coffee_shops
WHERE ST_DWithin(location, ST_Point(-122.4194, 37.7749), 1000)
ORDER BY distance_meters;

This query finds all coffee shops within 1000 meters (1km) of our reference point and orders them by distance.

Step 5: Visualize on a Map

To see your data on an actual map, export it as GeoJSON:

Copy this output and paste it into geojson.io to see your coffee shops plotted on an interactive map. That’s it—you’ve gone from zero to spatial queries in just 15 minutes!

Next Steps

Now that you have PostGIS up and running, here are some ideas to explore next:

  • Try ST_Buffer to create radius zones around points
  • Explore ST_Intersects for polygon overlaps
  • Import real-world data from OpenStreetMap
  • Build location-based features into your application

PostGIS opens up a world of spatial possibilities in PostgreSQL. From simple proximity searches to complex geographic analysis, you now have the foundation to build location-aware applications. Happy mapping!SELECT jsonb_build_object(
‘type’, ‘FeatureCollection’,
‘features’, jsonb_agg(
jsonb_build_object(
‘type’, ‘Feature’,
‘geometry’, ST_AsGeoJSON(location)::jsonb,
‘properties’, jsonb_build_object(‘name’, name)
)
)
)
FROM coffee_shops;

tial superpowers. This command loads all the PostGIS functions, data types, and operators into your current database.

Leave A Comment