2023-04-07

Building PostGIS and PgAdmin4 Docker Containers

Building PostGIS and PgAdmin4 Docker Containers

In this article, I will introduce how to set up PostGIS and PgAdmin4 using Docker containers and demonstrate how to connect to PostGIS through PgAdmin4's console.

Creating Docker Compose YML File

Create the following docker-compose.yml file:

docker-compose.yml
version: '3.1'
services:
  postgis:
    image: postgis/postgis
    container_name: postgis
    ports:
      - '5432:5432'
    environment:
      POSTGRES_PASSWORD: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data

  pgadmin4:
    image: dpage/pgadmin4
    container_name: pgadmin4
    ports:
      - '5050:80'
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@example.com
      PGADMIN_DEFAULT_PASSWORD: admin
    links:
      - postgis

volumes:
  pgdata:

Run the following command:

bash
$ docker compose up

This will start the containers for PostGIS and PgAdmin4.

Using PgAdmin4

Operate PgAdmin4 through the console and connect it to PostGIS.

First, open your browser and go to http://localhost:5050. Enter your login credentials to log in.

PgAdmin4 | 1

Click on "Add New Server."

PgAdmin4 | 2

Open the "Connection" tab and enter the authentication information to connect to PostGIS. Then click the "Save" button.

PgAdmin4 | 3

You now see that PostGIS is connected.

PgAdmin4 | 4

Troubleshooting PgAdmin4 Container

Container Not Starting

If you specify non-email characters for the PGADMIN_DEFAULT_EMAIL in the docker-compose.yml, the container will throw an error during startup.

docker-compose.yml
    environment:
        PGADMIN_DEFAULT_EMAIL: admin
        PGADMIN_DEFAULT_PASSWORD: admin
‘admin’ does not appear to be a valid email address. Please reset the PGADMIN_DEFAULT_EMAIL environment variable and try again.

To resolve this, modify PGADMIN_DEFAULT_EMAIL to a valid email format:

docker-compose.yml
    environment:
-        PGADMIN_DEFAULT_EMAIL: admin
+        PGADMIN_DEFAULT_EMAIL: admin@example.com
         PGADMIN_DEFAULT_PASSWORD: admin

Error When Operating pgAdmin

If you attempt to operate a PostgreSQL version that pgAdmin does not support, an error will occur.

Error retrieving the information – INTERNAL SERVER ERROR

ERROR: column rel.relhasoids does not exist
LINE 8: pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids,…

For example, pgAdmin 5.7 does not support PostgreSQL 14.

References

https://registry.hub.docker.com/r/postgis/postgis/
https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!