Working with geospatial data in Postgres.

PostgreSQL has several extensions so spatial and geometry data can be treated as first-class objects within your PostgreSQL database.

2 months ago   •   2 min read

By Katya Delaney

PostgreSQL has several extensions so spatial and geometry data can be treated as first-class objects within your PostgreSQL database.


The primary spatial-data extension is PostGIS. PostGIS (Geographic Information Systems) is an open-source extension of the PostgreSQL database that lets you work with geographic objects that integrate directly with your database. With PostGIS, geographic and spatial data can be treated as first-class objects in your database.

By adding the PostGIS extension to your PostgreSQL database, you can work seamlessly with geospatial data without having to convert that data from the format that the rest of your application is working with to use with your database. You can also determine relationships between that spatial data with the extension, such as the distance between two objects in your database. You can also use PostGIS to render visualizations of this data.

Working with data such as cities and geometry data is as simple as something like:

FROM city, superhero
WHERE ST_Contains(city.geom, superhero.geom)
AND = 'Gotham';

PostGIS includes:

  • Spatial Types
  • Point
  • Line
  • Polygon
  • Etc

The hierarchy of these spatial-focused type (from Introduction to PostGIS) is below:

  • Spatial-Indexing
  • Efficiently index spatial relationships
  • Spatial-Functions
  • For querying spatial properties, and the relationships between them
  • Functions for analyzing geometric components, determining spatial relationships, and manipulating geometries

In most databases, data is stored in rows and columns. With PostGIS, you can actually store data in a geometry column. This column stores data in a spatial coordinate system that’s defined by an SRID (Spatial Reference Identifier). This allows your database structure to reflect the spatial data that’s stored in the database.

There are other PostgreSQL extensions related to PostGIS for working with spatial data, too:

  • pgRouting - an extension of PostGIS itself; pgRouting enables geospatial routing information such as:

Shortest distance
Driving distance
Traveling salesman

  • ogrfdw - a data wrapper for reading other spatial and non-spatial datasources as tables in PostgreSQL
  • pgpointcloud A PostgreSQL extension and loader for storing Point Cloud data in PostgreSQL.
  • PointCloud data about the physical environment that is gathered using 3D cameras, and used in application areas such as AR, VR, and robotics

Example Scenarios

There are a variety of scenarios in which you may want to work with PostGIS and it’s related extensions for your application, including:

  • Working with census data
  • Storing addresses
  • Calculating the distance between two paths
  • Storing PointCloud data of the physical world
  • Tracking shipping data
  • Tracking cars and delivery vehicles
  • Visualization of raster data

PostgreSQL’s extensions for working with geospatial data allow you to treat that data as first-class objects in your database, leading to more powerful applications that can be built on top of data about the objects and relationships between them in the physical world.

Spread the word

Keep reading