Published on

Setting up postGIS on an existing postgres database inside a docker container

Authors

Motive behind writing this

  • Widely dispersed instructions/steps on how to install postGIS on existing postgres servers.
  • Many tutorial expects that you are starting a new project, or are using postGIS docker image directly.
  • The instructions on the official postgis installation are not clear for beginners. This could lead to many development hours wasted.

In this tutorial we'll walk through setting up postGIS on our existing postgres server which is running inside a docker container.

Prerequisites

Installation Steps

Lets assume that you have postgres docker-compose service db as mentioned below

version: '3.3'

services:
  ...

  db:
    image: postgres:13
    container_name: tutorial_postgres
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=tutorial_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  ...

volumes:
  db-data:

We want to add postGIS extension to this container. We'll follow along the official postgis installation documentation with some clear instructions.

  1. Lets get inside the running container
$ docker-compose exec db bash
  1. Make a directory called postgis_installation (folder name does not matter) and cd into it.
$ makedir postgis_installation
$ cd postgis_installation
  1. Install dependencies
$ apt update
$ apt-get install wget gcc libpq-dev libgeos-dev proj-bin libproj-dev make libxml2-dev libprotobuf-c-dev protobuf-c-compiler g++ postgresql-server-dev-13

| Note: These are all required for the default setup of postGIS

  1. Download postGIS source and unpack
$ wget http://postgis.net/stuff/postgis-3.1.2dev.tar.gz
$ tar -xvzf postgis-3.1.2dev.tar.gz
  1. Check if all dependencies are installed
$ cd postgis-3.1.2dev
$ ./configure

If everything goes well /.configure will output the following

PostGIS is now configured for x86_64-pc-linux-gnu
  1. Build postGIS
# /postgis_installation/postgis-3.1.2dev
$ make

If everything goes well make will output the following

PostGIS was built successfully. Ready to install.
  1. Install postGIS
# /postgis_installation/postgis-3.1.2dev
$ make install
  1. Check if postGIS got installed by going into psql shell (here -U is the POSTGRES_USER for the container)
$ psql -U postgres

Inside the psql shell run the following command and you should see an output like this

postgres=# SELECT name, default_version, installed_version FROM pg_available_extensions
WHERE name LIKE 'postgis%' or name LIKE 'address%';

             name             | default_version | installed_version
------------------------------+-----------------+-------------------
 address_standardizer_data_us | 3.1.2dev        |
 postgis_tiger_geocoder       | 3.1.2dev        |
 postgis_raster               | 3.1.2dev        |
 postgis_topology             | 3.1.2dev        |
 address_standardizer         | 3.1.2dev        |
 postgis                      | 3.1.2dev        |
(6 rows)

  1. Awesome, now we just need to enable postGIS extension on the schema/database. Run the following commands on the psql shell
\connect tutorial_db
\x
CREATE EXTENSION postgis;
\dx postgis*

The last command should output this

Name        | postgis
Version     | 3.1.2dev
Schema      | public
Description | PostGIS geometry and geography spatial types and functions

This is it!! We have successful installed postGIS on an existing postgres instance. Remember to save/tag the docker image so you can reuse this image if the container dies.

Optional Steps

Install pgAdmin to get high level overview of your database. Edit your docker-compose file and add the following service.

version: '3.3'

services:
  ...

  db:
    image: postgres:13
    container_name: tutorial_postgres
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=tutorial_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  ...
  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    restart: always
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    ports:
    - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
  ...

volumes:
  db-data:
  pgadmin-data:

After docker-compose up is running, head to http://localhost:5050 and login with the pgAdmin credentials provided in docker-compose file.

Add a new server with db as the host name. In the left side you'll see your tutorial_db and in Extentions dropdown you'll start seeing postGIS extension.

Isn't it cool, eh?