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:
- Postgres Permissions – Create, Update, Delete Database Users
- Running Postgresql 14 with Docker and Docker-Compose
- How to Install and Configure Postgres 14 on Rocky Linux 9
Prerequisites
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- 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:
|
|
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:
|
|
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 port80
is mapped to host port5080
. That means the service can be accessed throughlocalhost:5432
- The
-v
directive is used to mount volumes. In our case we are mounting the container volume/var/lib/pgadmin
to 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:
|
|
If you need to clean up the container when not in use, you can stop and remove the container using this command:
|
|
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
:
|
|
Now start the containers:
|
|
The commands:
up
brings up the container-d
in a detached mode
Verify the container processes using the ps command:
|
|
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:
|
|
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
.