Introduction
PostGIS is an extension that turns PostgreSQL into a spatial database capable of storing, indexing, and querying geographic data. It implements the OGC Simple Features specification and adds hundreds of spatial functions, making it possible to perform complex GIS operations entirely within SQL.
What PostGIS Does
- Stores point, line, polygon, and raster geometry data types in PostgreSQL
- Provides 300+ spatial functions for analysis, measurement, and transformation
- Enables spatial indexing via GiST for fast geographic queries
- Supports coordinate system transformations between 6000+ projections
- Handles 3D geometries, topology, and geocoding
Architecture Overview
PostGIS installs as a PostgreSQL extension that registers custom data types (geometry, geography, raster) and operators. Spatial indexing uses GiST (Generalized Search Tree) indexes that store bounding boxes for efficient spatial filtering. The extension links against GEOS for geometry operations, PROJ for coordinate transformations, and GDAL for raster format support.
Self-Hosting & Configuration
- Install via system packages:
apt install postgresql-16-postgis-3 - Enable per-database:
CREATE EXTENSION postgis; - Add raster support:
CREATE EXTENSION postgis_raster; - Configure shared_preload_libraries for raster out-db access
- Use official Docker images with PostGIS pre-installed
Key Features
- Spatial indexing with R-tree via GiST for sub-millisecond queries
- Geography type for accurate calculations on the spheroid
- Raster analysis for satellite imagery and elevation data
- Network routing support via pgRouting extension
- Integration with every major GIS tool (QGIS, GeoServer, GDAL)
Comparison with Similar Tools
- SpatiaLite — SQLite-based spatial DB; PostGIS offers better concurrency and indexing
- MongoDB Geospatial — Document-based with basic geo queries; PostGIS has richer spatial functions
- H3 (Uber) — Hexagonal grid indexing system; PostGIS is a full spatial database
- Tile38 — In-memory geospatial DB for real-time; PostGIS for persistent analytical workloads
FAQ
Q: What is the difference between geometry and geography types? A: Geometry uses planar coordinates (faster math). Geography uses spheroidal coordinates (accurate for large distances). Use geography for global data, geometry for local projections.
Q: How do I import shapefiles into PostGIS?
A: Use shp2pgsql (bundled) or ogr2ogr (from GDAL): ogr2ogr -f PostgreSQL PG:dbname=mydb shapefile.shp
Q: Does PostGIS support GeoJSON? A: Yes. Use ST_AsGeoJSON() to export and ST_GeomFromGeoJSON() to import geometries in GeoJSON format.
Q: How large can PostGIS datasets be? A: PostGIS inherits PostgreSQL limits. Tables with hundreds of millions of geometries are common in production when properly indexed.