Run the PostGIS Server as a Docker Container

It is no doubt that Docker has become one of the most widely used developer tool in recent years. Docker allows us to develop, package, ship, and run the applications in isolated environments that contains all the dependencies that are required to run the application.

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:

  1. Installed Docker Engine on your system. You can check our tutorials on how to install Docker on your target system.
  2. 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:

$ docker pull postgis/postgis

Once we have the image downloaded, we can run the container using the image as shown in the following command:

$ docker run --name postgis-server -e <strong>POSTGRES_PASSWORD</strong>=password -d -p 5432:5432 postgis/postgis

In the given command, we specify the following parameters:

  1. –name – This allows us to specify the name of the container.
  2. -e – This allows us to set the PostgreSQL password as an environment variable. This is the password that is used for the postgres user.
  3. -d – This tells Docker to run the container in the background or detached mode.
  4. -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:

$ docker ps

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:

$ mkdir -p ~/data
$ cd ~/data
$ unzip

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:

$ docker exec -it postgis-server psql -U postgres -d postgres

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.

postgres=# shp2pgsql -I -s 4326 ne_110m_admin_0_countries | psql -U postgres -d postgres

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:

$ docker exec -it postgis-container psql -U postgres -d postgres

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list