PostgreSQL: Use Docker Compose for PGadmin4

PostgreSQL: Use Docker Compose for PGadmin4

pgAdmin4 is a great tool to manage PostgreSQL databases. It can be installed locally; when you execute it, it starts a local webserver, and you can connect to it.

But if there are more than one or two DBAs, it could be a benefit to install a central pgAdmin4. We'll use Docker for that.

Installation

This is my docker-compose.yml file:

services:
  pgadmin:
    image: dpage/pgadmin4:snapshot
    container_name: pgadmin4
    restart: always
    ports:
      - 80:80
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@example.net
      PGADMIN_DEFAULT_PASSWORD: XxXxXxXx
    volumes:
      - ./pgadmin-data:/var/lib/pgadmin

Let's pull the image and start the container:

# docker compose pull
# docker compose up -d
[+] Pulling 17/17
 ✔ pgadmin Pulled                                                                              9.7s
   ✔ cb8611c9fe51 Already exists                                                               0.0s
   ✔ 17313cc95e95 Pull complete                                                                5.8s
   ✔ b4fff8eeea2c Pull complete                                                                5.8s
   ✔ b4eda369ea3c Pull complete                                                                5.9s
   ✔ 6886a9683b1f Pull complete                                                                5.9s
   ✔ 1efdb2ce4d2f Pull complete                                                                5.9s
   ✔ d2868dda862c Pull complete                                                                5.9s
   ✔ b9f8bc43b7f3 Pull complete                                                                6.0s
   ✔ e46935ae5e60 Pull complete                                                                6.7s
   ✔ 45db73b4f034 Pull complete                                                                7.0s
   ✔ dcbdaa914ac1 Pull complete                                                                7.0s
   ✔ ed377bef63dd Pull complete                                                                7.0s
   ✔ 55c17a7b26f0 Pull complete                                                                7.0s
   ✔ 584759e4668e Pull complete                                                                7.1s
   ✔ 3bb246f2fe2f Pull complete                                                                7.1s
   ✔ 30b7438627ac Pull complete                                                                8.0s
[+] Running 1/1
 ✔ Container pgadmin4  Started   

Then we look at the log files:

pgadmin4  | email config is {'CHECK_EMAIL_DELIVERABILITY': False, 'ALLOW_SPECIAL_EMAIL_DOMAINS': []}
pgadmin4  | ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
pgadmin4  |            [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'
pgadmin4  | HINT   : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
pgadmin4  |          'pgadmin', and try again, or, create a config_local.py file
pgadmin4  |          and override the SESSION_DB_PATH setting per
pgadmin4  |          https://www.pgadmin.org/docs/pgadmin4/8.14/config_py.html
pgadmin4  | postfix/postlog: starting the Postfix mail system
pgadmin4  | [2025-01-23 13:12:42 +0000] [1] [INFO] Starting gunicorn 22.0.0
pgadmin4  | [2025-01-23 13:12:42 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
pgadmin4  | [2025-01-23 13:12:42 +0000] [1] [INFO] Using worker: gthread
pgadmin4  | [2025-01-23 13:12:42 +0000] [91] [INFO] Booting worker with pid: 91
pgadmin4  | [2025-01-23 13:12:46 +0000] [91] [INFO] Worker exiting (pid: 91)
pgadmin4  | ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
pgadmin4  |            [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'

We can see an error here:

pgadmin4  | ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
pgadmin4  |            [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'

The docker compose file configures the volume like this:

    volumes:
      - ./pgadmin-data:/var/lib/pgadmin

The website for pgAdmin4 tells us to change permissions of the volume:

# chmod -R 5050:5050 ./pgadmin-data

After that, we restart the container, and everything's ok:

# docker compose stop
# docker compose up -d

Now, we can connect to we web login using http://<host name> and the login data we defined previously in the Docker Compose file.

Networks

To enable communication between the different containers in Docker, we'll have to configure all containers to use the same network:

networks:
  pgadmin4:
    name: pgadmin4
    external: true

services:
  psql_17:
    image: postgres:17
    hostname: psql_17
    restart: always
    environment:
      POSTGRES_PASSWORD: XxXxXxXx
    networks:
      - pgadmin4

Also the pgAdmin4 container has to use this network, so here's my modified docker-compose.yml:

networks:
  pgadmin4:
    name: pgadmin4
    external: true

services:
  pgadmin:
    image: dpage/pgadmin4:snapshot
    container_name: pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@example.net
      PGADMIN_DEFAULT_PASSWORD: XxXxXxXx
    volumes:
      - ./pgadmin-data:/var/lib/pgadmin
    networks:
      - pgadmin4

After that, database can added into pgAdmin4:

Old or unsupported versions

Version: 9.2

When I try to connect to PostgreSQL Version 9.2, I'll get this error:

But 9.2 is just too old...

Version: 9.4

When connection to Version 9.4, I get this warning:

But the connection is successful.