How to run PGAdmin 4 Using Docker and Docker-Compose

PGAdmin is a web-based GUI tool used to interact with the Postgres database sessions, both locally and remote servers as well. You can use PGAdmin to perform any sort of database administration required for a Postgres database.

pgAdmin 4 is designed to meet the needs of both novice and experienced Postgres users alike, providing a powerful graphical interface that simplifies the creation, maintenance and use of database objects.

In this guide, we are going to set up pgAdmin 4 in a container, the web based administration tool for the PostgreSQL database.

Related content:

Prerequisites

To follow along, ensure you have the following:

  1. Docker installed locally and permissions to use it to launch containers
  2. Docker compose is installed locally
  3. Basic knowledge of the command line

PostgreSQL Utilities

The PostgreSQL utilities pg_dump, pg_dumpall, pg_restore and psql are included in the container to allow backups to be created and restored and other maintenance functions to be executed. Multiple versions are included in the following directories to allow use with different versions of the database server:

  • PostgreSQL 10: /usr/local/pgsql-10
  • PostgreSQL 11: /usr/local/pgsql-11
  • PostgreSQL 12: /usr/local/pgsql-12
  • PostgreSQL 13: /usr/local/pgsql-13
  • PostgreSQL 14: /usr/local/pgsql-14

The default binary paths set in the container are as follows:

1
2
3
4
5
6
7
DEFAULT_BINARY_PATHS = {
    'pg-14': '/usr/local/pgsql-14',
    'pg-13': '/usr/local/pgsql-13',
    'pg-12': '/usr/local/pgsql-12',
    'pg-11': '/usr/local/pgsql-11',
    'pg-10': '/usr/local/pgsql-10'
}

this may be changed in the Preferences Dialog.

Environment Variables

The container will accept the following variables at startup:

PGADMIN_DEFAULT_EMAIL

This is the email address used when setting up the initial administrator account to login to pgAdmin. This variable is required and must be set at launch time.

PGADMIN_DEFAULT_PASSWORD

This is the password used when setting up the initial administrator account to login to pgAdmin. This variable is required and must be set at launch time.

PGADMIN_DEFAULT_PASSWORD_FILE

This is the password used when setting up the initial administrator account to login to pgAdmin. This value should be set to docker secret in order to set the password. This variable is supported in docker swarm environment or while creating container with docker compose. PGADMIN_DEFAULT_PASSWORD or PGADMIN_DEFAULT_PASSWORD_FILE variable is required and must be set at launch time.

PGADMIN_LISTEN_ADDRESS

Default: [::]

Specify the local address that the servers listens on. The default should work for most users - in IPv4-only environments, this may need to be set to 0.0.0.0.

_### PGADMIN_LISTEN_PORT

Default: 80 or 443 (if TLS is enabled)

Allows the port that the server listens on to be set to a specific value rather than using the default.

Check more info here.

Running PGAdmin with Docker

You can run PGAdmin with docker run command like shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ docker run -p 5080:80 \
    --name my-pgadmin \
    -e 'PGADMIN_DEFAULT_EMAIL=admin@citizix.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' \
    -v ~/pgadmin:/var/lib/pgadmin \
    -d dpage/pgadmin4:6.12

Unable to find image 'dpage/pgadmin4:6.12' locally
6.12: Pulling from dpage/pgadmin4
Digest: sha256:781369df9994157bbcee66d643fd2f70e996e95f86bbe9ec5aa17d7c4e7c3c43
Status: Downloaded newer image for dpage/pgadmin4:6.12

3c5de66427522352c15cc885a14f1e9074b1469f0eeb8d4c48de001bc41d6b2d

In the above command:

  • The -d instructs docker container to run as a detached process. It run container in background and print container ID
  • -p is for port mapping. We are instructing the container to expose the container port externally. Container port 80 is mapped to host port 5080. That means the service can be accessed through localhost:5432
  • The -v directive is used to mount volumes. In our case we are mounting the container volume /var/lib/pgadminto host path ~/pgadmin. Containers are ephemeral devices that will contain its data for the time it is running. Once a container is stopped, its data is lost. Mounting volumes ensures that the data is added to a host path that can be reused when the container is restarted.
  • The -e argument is for the environment variables. The supplied environment variables will be used to set up a default email and default password.

To check that our container is running as expected, use the docker ps command:

1
docker ps

If you need to clean up the container when not in use, you can stop and remove the container using this command:

1
2
3
4
docker stop my-pgadmin

# Removing
docker rm my-pgadmin

To access PGAdmin, open your favourite browser and head over to http://127.0.0.1:5080/ then login with the username and password set up in environment variables.

Running PGAdmin with Docker-Compose

We can achieve the same functionality with docker-compose. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

Docker Compose allows you to define the service (PGAdmin in our case) with properties like the image to use, ports to expose, volumes to mount and environment variables.

Here is how we would use docker-compose to achieve the functionality above. Save this as docker-compose.yaml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
version: "3.9"

services:
  pgadmin:
    image: dpage/pgadmin4:6.12
    ports:
      - 5080:80
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@citizix.com
      - PGADMIN_DEFAULT_PASSWORD=SuperSecret
    volumes:
      - pgadmin_data:/home/rundeck/server/data

volumes:
  pgadmin_data:

Now start the containers:

1
2
3
4
$ docker-compose up -d

Creating network "blog_default" with the default driver
Creating blog_pgadmin_1 ... done

The commands:

  • up brings up the container
  • -d in a detached mode

Verify the container processes using the ps command:

1
docker-compose ps

HTTP via Nginx

A configuration similar to the following can be used to create a simple HTTP reverse proxy listening for all hostnames with Nginx:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
server {
    listen 80;
    server_name pgadmin.citizix.com;

    location / {
        proxy_set_header Host $host;
        proxy_pass http://localhost:5050/;
        proxy_redirect off;
    }
}

Conclusion

In this guide we managed to run PGAdmin 4 as a docker container in our system, we explored using the docker run command while passing the required arguments an alternative approach of simplifying the process with docker-compose.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy