PostGIS is an extension for the popular relational database PostgreSQL. It adds support for storing, indexing and querying geographic data.
There are a number of reasons why you might want to use PostGIS but the primary advantage of using a database over files is the fact that multiple people can access, query and update information in a database at the same time.
To get up and running fast we’re going to use an app called Postgres.app which includes PostGIS. You can download it from https://postgresapp.com
While this app provides a great way to start learning PostGIS, it’s likely not what you should use for an enterprise level application.
Before we can do anything we first need to enable the PostGIS extension in our database.
Double-click the database named after your username and it will bring up a psql
session in a terminal window. psql
is a command-line tool for interacting with a Postgres database.
Enter the following commands to enable the PostGIS Extension on the database named with your username. In my case it’s named brian
.
CREATE SCHEMA postgis;
CREATE EXTENSION postgis SCHEMA postgis;
CREATE EXTENSION postgis_raster SCHEMA postgis;
ALTER DATABASE brian SET search_path=public,postgis;
exit
Schemas are namespaces that help you organize the tables, views and other things you create in your database. We created a schema named postgis
so that all the information associated with the PostGIS extensions won’t pollute or collide with other schemas we create. The extensions will still be available to all other schemas in the database.
Now that we have a database running with the PostGIS extension installed we can connect to it from QGIS:
As I mentioned previously, schemas are a convenient way to organize the tables, views and other database objects you create. Let’s create a dedicated schema called tutorial
for the work we’ll be doing today:
Now that we have a dedicated schema, let’s create a table to hold some point data:
points
table in your tutorial
schema to add it as a layer to your mapOnce we have our points table as a layer in our project we can add data like we would with a GeoPackage or Shapefile:
points
layerAnother thing we can do with PostGIS is import data from other formats, such as Shapefiles. Lets import that US States and Territories from weather.gov:
s_08mr23.shp
us_states_and_territories
table under the tutorial schema in the Browser to add it as a layer to your mapYou’ve now have a PostGIS database running on your machine and you know how to create your own schemas, create your own tables and import data using QGIS.
There are a couple of books you might want to check out — one I’ve been working through and the other I haven’t picked up yet:
If you’re interested in learning more please follow me on YouTube. If you have any questions or suggestions, feel free to reach out to me on Mastodon.