PostGIS is an extension of the PostgreSQL database that adds support for geographic data objects. This allows us to use the PostgreSQL database to store and query a spatial data. As you can guess, combining PostGIS and Docker provides an efficient and extensive method of managing a spatial data.
In this tutorial, we will learn how we can quickly setup a PostGIS instance inside a Docker container. We will also cover some PostGIS basics and learn how to load a spatial data and perform some basic queries using the container.
To follow along with this tutorial, ensure that you have the following:
- Installed Docker Engine on your system. You can check our tutorials on how to install Docker on your target system.
- Basic knowledge of PostgreSQL and spatial data
With the given requirements met, we can proceed with the tutorial.
Run PostGIS in Docker
Let us proceed and setup the PostGIS container. We start by pulling the PostGIS container from the Docker Hub with the following command:
Once we have the image downloaded, we can run the container using the image as shown in the following command:
In the given command, we specify the following parameters:
- –name – This allows us to specify the name of the container.
- -e – This allows us to set the PostgreSQL password as an environment variable. This is the password that is used for the postgres user.
- -d – This tells Docker to run the container in the background or detached mode.
- -p 5432:5432 – This allows us to map the port 5432 inside the container to the host on port 5432.
To verify that the container is running, use the following command:
You should see the PostGIS container listed.
Load the Spatial Data in PostGIS
Now that we have the PostGIS container running, we can load the spatial data using various tools and format such as GeoJSON, CSV, etc.
For this case, we use a Shapefile as shown in the following commands:
$ cd ~/data
$ unzip ne_110m_admin_0_countries.zip
In the given commands, we start by creating a directory to store the data that we wish to load. We then download a geo data from the specified link and extract it into the directory.
The next step is to load the data into the database. We start by connecting to the PostgreSQL database using the following command:
If prompted for a password, provide the password that you configured when running the container. This should authenticate you and drop you into the PostgreSQL shell.
Once connected to the database, we can use the “shp2pgsql” utility to import the data from the Shapefiles that we extracted in the previous steps.
In the previous command, we use the -I option to create a spatial index for a faster querying of the spatial data. We also specify the coordinate system as 4326 using the -s parameter. The EPSG:4326 is the standard coordinate state system for WGS 84.
PostGIS Query Data
Once we loaded the data, we can exit the PSQL utility by running the “\q” command or exit.
Next, to verify we that have the data loaded, we can reconnect to the database by running the following command:
Next, you can run the basic spatial queries to verify that you have the data stored.
In this tutorial, you learned how to run the PostGIS server as a Docker container and load the data into the database.