Install PostGIS Inside Docker Container on Linux

Install PostGIS Inside Docker Container on Linux

PostGIS is an open-source spatial database extension for PostgreSQL, the popular relational database management system. It enhances PostgreSQL by adding support for geographic objects and spatial data types, enabling advanced geographic and location-based queries.

This tutorial explains how to install PostGIS inside a Docker container on Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Install PostGIS

  • Host network

Run the following command to create a container for PostGIS that uses host network:

docker run -d --name=postgis --restart=always --network=host \
    -v /opt/postgis/data:/var/lib/postgresql/data \
    -e POSTGRES_PASSWORD=pwd123 \
    postgis/postgis
  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, PostGIS service is listening on port 5432. It can be changed with -p option.

docker network create app-net
docker run -d --name=postgis --restart=always --network=app-net \
    -p 8080:5432 \
    -v /opt/postgis/data:/var/lib/postgresql/data \
    -e POSTGRES_PASSWORD=pwd123 \
    postgis/postgis

Note: don't forget to change postgres superuser password.

Testing PostGIS

Run the following command to launch the PostgreSQL client (psql) and list all the databases available on the server:

docker exec -it postgis psql -U postgres -l

Output example:


                                                        List of databases
       Name       |  Owner   | Encoding | Locale Provider |  Collate   |   Ctype    | Locale | ICU Rules |   Access privileges
------------------+----------+----------+-----------------+------------+------------+--------+-----------+-----------------------
 postgres         | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           |
 template0        | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/postgres          +
                  |          |          |                 |            |            |        |           | postgres=CTc/postgres
 template1        | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/postgres          +
                  |          |          |                 |            |            |        |           | postgres=CTc/postgres
 template_postgis | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           |
(4 rows)

Uninstall PostGIS

To completely remove PostGIS, remove its container:

docker rm --force postgis

Remove PostGIS image:

docker rmi postgis/postgis

You can also remove PostGIS data:

sudo rm -rf /opt/postgis

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.