How to Use PostGIS with Python: Step-by-Step Tutorial for Geospatial Data
- Anvita Shrivastava
- 1 day ago
- 4 min read
Geospatial data serves as the foundation for many current-day applications such as GPS-based navigation, package tracking, urban development, and environmental assessment. PostgreSQL is one of the most powerful relational databases available. By adding PostGIS to PostgreSQL, it becomes an extremely capable spatial database that can house, query, and analyze geospatial data.
PostGIS, in conjunction with the extensive geospatial capabilities of Python, creates a powerful framework for developing widespread Geographic Information Systems (GIS), performing spatial data analysis, and building location-based products.

What Is PostGIS?
PostGIS provides PostgreSQL with capabilities for working with location data by adding geographic capabilities and allowing for the storage and querying of location information within the same database as your other information.
PostGIS provides support for:
point, line, and polygon objects
multi-polygon objects
spatial index support
Coordinate Reference Systems (CRS)
distance calculations
buffer calculations
intersection queries
spatial joins
What is different about using PostGIS instead of just storing your latitude and longitude values separately is that you will be able to store complete geometry objects for a location and make use of these in any of the applications that support spatial analysis.
Why Use PostGIS with Python?
Due to the large number of GIS libraries in Python's ecosystem, Python has become the language of choice for geospatial analysis.
Some of the advantages of using Python include:
Fast processing of spatial data
Advanced analysis capabilities
Automated workflow
Integration of machine learning tools
Interactive maps
Scalable back-end applications
Some of the popular Python libraries for use in GIS applications are:
Using Python and PostGIS provides an enterprise-level geospatial solution.
Prerequisites
Before getting started, install:
PostgreSQL
PostGIS
Python 3.10+
pip
Install required Python packages:
pip install psycopg2-binary sqlalchemy geopandas shapely pandas matplotlib foliumStep 1: Install PostgreSQL and PostGIS
Install PostgreSQL on your operating system.
Ubuntu
sudo apt update
sudo apt install postgresql postgismacOS
brew install postgresql
brew install postgisWindows
Download the PostgreSQL installer and select the PostGIS extension during installation.
Verify the installation:
SELECT PostGIS_Version();Example output:
3.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1Step 2: Create a Spatial Database
Create a new PostgreSQL database.
CREATE DATABASE gisdb;Connect to the database:
psql gisdbEnable PostGIS:
CREATE EXTENSION postgis;Verify:
SELECT PostGIS_Full_Version();Step 3: Connect PostGIS to Python
Using Psycopg:
import psycopg2
conn = psycopg2.connect(
database="gisdb",
user="postgres",
password="password",
host="localhost",
port="5432"
)
cursor = conn.cursor()
cursor.execute("SELECT version();")
print(cursor.fetchone())
conn.close()Connection successful!
Step 4: Create a Spatial Table
Let's create a table containing city locations.
CREATE TABLE cities (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
location GEOMETRY(Point, 4326)
);Here:
Point represents latitude and longitude.
4326 is the WGS84 coordinate reference system used by GPS.
Step 5: Insert Geospatial Data
Insert spatial data using SQL.
INSERT INTO cities (name, location)
VALUES
('New York', ST_Point(-74.0060,40.7128)),
('Chicago', ST_Point(-87.6298,41.8781)),
('Los Angeles', ST_Point(-118.2437,34.0522));Or insert using Python.
cursor.execute("""
INSERT INTO cities(name, location)
VALUES (
%s,
ST_GeomFromText(%s,4326)
)
""",
("Seattle",
"POINT(-122.3321 47.6062)"))
conn.commit()Step 6: Read Spatial Data
Query the table.
cursor.execute("""
SELECT
name,
ST_AsText(location)
FROM cities;
""")
rows = cursor.fetchall()
for row in rows:
print(row)Output:
('New York', 'POINT(-74.006 40.7128)')
('Chicago', 'POINT(-87.6298 41.8781)')
('Los Angeles', 'POINT(-118.2437 34.0522)')Step 7: Load Data into GeoPandas
GeoPandas makes reading PostGIS incredibly simple.
import geopandas as gpd
from sqlalchemy import create_engine
engine = create_engine(
"postgresql://postgres:password@localhost/gisdb"
)
gdf = gpd.read_postgis(
"SELECT * FROM cities",
engine,
geom_col="location"
)
print(gdf.head())The geometry column becomes a GeoDataFrame automatically.
Step 8: Calculate Distances
Find the distance between two cities.
SELECT
ST_Distance(
a.location::geography,
b.location::geography
)
FROM cities a,
cities b
WHERE
a.name='New York'
AND
b.name='Chicago';Returns the distance in meters.
Step 9: Find Nearby Locations
Search for cities within 500 km.
SELECT
name
FROM cities
WHERE
ST_DWithin(
location::geography,
ST_Point(-74.0060,40.7128)::geography,
500000
);This is one of the most common location-based queries.
Step 10: Create Spatial Indexes
Spatial indexes dramatically improve query performance.
CREATE INDEX idx_city_location
ON cities
USING GIST(location);Always index geometry columns for production workloads.
Step 11: Perform Spatial Joins
Suppose you have two tables:
cities
states
Find which state each city belongs to.
SELECT
c.name,
s.state_name
FROM cities c
JOIN states s
ON ST_Within(
c.location,
s.geometry
);Spatial joins are among the most powerful features of PostGIS.
Step 12: Buffer Analysis
Create a 10 km radius around a city.
SELECT
ST_Buffer(
location::geography,
10000
)
FROM cities
WHERE name='Chicago';Buffer analysis is useful for:
Delivery coverage
Service areas
Emergency planning
Infrastructure analysis
Step 13: Export Data to GeoJSON
GeoJSON is widely used in web mapping applications.
SELECT
ST_AsGeoJSON(location)
FROM cities;Output:
{
"type": "Point",
"coordinates":[
-74.006,
40.7128
]
}Step 14: Visualize Data Using Folium
import folium
m = folium.Map(
location=[39,-98],
zoom_start=4
)
for idx, row in gdf.iterrows():
folium.Marker(
[row.geometry.y, row.geometry.x],
popup=row["name"]
).add_to(m)
m.save("cities.html")Open the generated HTML file in your browser to view an interactive map.
The combination of PostGIS and Python can help create modern, sophisticated geospatial apps. With PostGIS as a spatial database (for storing, indexing, and analyzing spatial data) and Python as a framework for automating, visualizing, and analyzing data, the two technologies can be combined to create powerful geospatial applications.
The use of PostGIS together with other libraries, including GeoPandas, Shapely, SQLAlchemy, and Psycopg, allows developers to build applications that support mapping and spatial analysis quickly; this may also include developing applications for logistics, urban development, environmental analysis, and location intelligence.
As you develop apps that utilize geolocation, analyze geographic data, or build enterprise-level GIS applications, having learned how to use PostGIS in conjunction with Python will provide you with an invaluable skill set that enables processing geospatial data at scale and with high performance.
To learn more about PostGISÂ and its geospatial capabilities, click here.
For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:
Email: info@geowgs84.com
USA (HQ): (720) 702–4849
(A GeoWGS84 Corp Company)
