Configure PostgreSQL and pgAdmin Docker Containers - CentLinux


Thursday, 5 March 2020

Configure PostgreSQL and pgAdmin Docker Containers


PostgreSQL is a free and open source RDBMS (Relational Database Management System). PostgreSQL (or postgres) is emphasizing on extensibility and technical standards. PostgreSQL is the default database for macOS Server and it is also available for Linux, FreeBSD, OpenBSD and Windows.

We have already installed PostgreSQL on CentOS and Ubuntu Servers. Now, we are going to use the Docker containerization platform to configure PostgreSQL and pgAdmin Docker containers.

If you are new to Docker platform, then you should read Docker in Action by Manning Publications.


This Article Provides:


    Environment Specification:

    We are using a Ubuntu Server based Docker Host with following specification.

    • CPU - 3.4 Ghz (2 cores)
    • Memory - 2 GB
    • Storage - 20 GB
    • Operating System - Ubuntu Server 18.04 LTS
    • Hostname –
    • IP Address - /24

    We are using a pre-configured Docker host in this article. For setting up the required environment, please follow our previous articles to install Docker on Ubuntu Server or install Docker on CentOS.


    Configure PostgreSQL Docker Container:

    Connect with as a privileged user by using a ssh tool like PuTTY.

    Create a directory to store configuration and data files related to PostgreSQL Docker container.

    ahmer@docker-01:~$ mkdir ~/postgres-01 ahmer@docker-01:~$ cd postgres-01

    Create a directory for PostgreSQL data files.

    ahmer@docker-01:~/postgres-01$ mkdir postgres_data

    Create a docker-compose.yml file.

    ahmer@docker-01:~/postgres-01$ vi docker-compose.yml

    And define postgres service therein.

    version: "3.1" services: db: image: "postgres:11" container_name: "" ports: - "5432:5432" environment: POSTGRES_PASSWORD: "123" volumes: - ./postgres_data:/var/lib/postgresql/data links: - "pgadmin"

    Pull the required postgres image from Docker Hub.

    ahmer@docker-01:~/postgres-01$ sudo docker image pull postgres:11 11: Pulling from library/postgres 6d28e14ab8c8: Pull complete 64da04b335f8: Pull complete c2c855795327: Pull complete 1de39762bafa: Pull complete e72de942a5bc: Pull complete 5ed4b3beed09: Pull complete bc4fa92d5de6: Pull complete 881ba21508a4: Pull complete a1f3f1480763: Pull complete 0a652322d262: Pull complete a4c4345d4a61: Pull complete 84378331cde0: Pull complete e4b51862ee73: Pull complete ef2d3c443e8e: Pull complete Digest: sha256:bd4aafe95150214ecb2388f6b16eb2e89ebe18f10831ff9d2fc810821fc481c5 Status: Downloaded newer image for postgres:11

    Starting postgres container using our docker-compose.yml file.

    ahmer@docker-01:~/postgres-01$ sudo docker-compose up [sudo] password for ahmer: Creating ... done Attaching to | The files belonging to this database system will be owned by user "postgres". | This user must also own the server process. | | The database cluster will be initialized with locale "en_US.utf8". | The default database encoding has accordingly been set to "UTF8". | The default text search configuration will be set to "english". | | Data page checksums are disabled. | | fixing permissions on existing directory /var/lib/postgresql/data ... ok | creating subdirectories ... ok | selecting default max_connections ... 100 | selecting default shared_buffers ... 128MB | selecting default timezone ... Etc/UTC | selecting dynamic shared memory implementation ... posix | creating configuration files ... ok | running bootstrap script ... ok | performing post-bootstrap initialization ... ok | syncing data to disk ... ok | | Success. You can now start the database server using: | | | WARNING: enabling "trust" authentication for local connections | You can change this by editing pg_hba.conf or using the option -A, or | --auth-local and --auth-host, the next time you run initdb. | pg_ctl -D /var/lib/postgresql/data -l logfile start | | waiting for server to start....2020-03-04 18:48:31.011 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" | 2020-03-04 18:48:31.054 UTC [47] LOG: database system was shut down at 2020-03-04 18:48:30 UTC | 2020-03-04 18:48:31.098 UTC [46] LOG: database system is ready to accept connections | done | server started | | /usr/local/bin/ ignoring /docker-entrypoint-initdb.d/* | | waiting for server to shut down...2020-03-04 18:48:31.181 UTC [46] LOG: received fast shutdown request | .2020-03-04 18:48:31.183 UTC [46] LOG: aborting any active transactions | 2020-03-04 18:48:31.186 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1 | 2020-03-04 18:48:31.188 UTC [48] LOG: shutting down | 2020-03-04 18:48:31.236 UTC [46] LOG: database system is shut down | done | server stopped | | PostgreSQL init process complete; ready for start up. | | 2020-03-04 18:48:31.296 UTC [1] LOG: listening on IPv4 address "", port 5432 | 2020-03-04 18:48:31.296 UTC [1] LOG: listening on IPv6 address "::", port 5432 | 2020-03-04 18:48:31.314 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" | 2020-03-04 18:48:31.343 UTC [55] LOG: database system was shut down at 2020-03-04 18:48:31 UTC | 2020-03-04 18:48:31.371 UTC [1] LOG: database system is ready to accept connections

    Open a new ssh session and connect with postgres container.

    ahmer@docker-01:~$ sudo docker exec -it bash root@7bb0d4f1e4a6:/# su - postgres postgres@7bb0d4f1e4a6:~$ psql psql (11.7 (Debian 11.7-2.pgdg90+1)) Type "help" for help. postgres=# exit postgres@7bb0d4f1e4a6:~$ exit logout root@7bb0d4f1e4a6:/# exit exit

    PostgreSQL docker container is successfully configured.


    Configure pgAdmin 4 Docker Container:

    Pull the pgAdmin 4 image from Docker Hub.

    ahmer@docker-01:~/postgres-01$ sudo docker pull dpage/pgadmin4 Using default tag: latest latest: Pulling from dpage/pgadmin4 89d9c30c1d48: Pull complete 910c49c00810: Pull complete 7efe415eb85a: Pull complete 7d8d53519b81: Pull complete 519124ac136c: Pull complete fbfa5cf626f8: Pull complete f53a64187e16: Pull complete feae0c230730: Pull complete 87ae2307a1ad: Pull complete 0a609eb1a1ca: Pull complete b8bb05efc354: Pull complete bccaa4da228f: Pull complete 084101fb937a: Pull complete 774874c72d7a: Pull complete 45a1fa7f66b7: Pull complete b19cea40abf6: Pull complete 76e3d6803955: Pull complete Digest: sha256:1141073018353f91953c1523f170821e139dbd1c2d7808d3804962b2ba7e89e3 Status: Downloaded newer image for dpage/pgadmin4:latest

    Edit docker-compose.yml file and add pgadmin service.

    ahmer@docker-01:~/postgres-01$ vi docker-compose.yml

    Now define pgadmin service under the services section.

    pgadmin: image: "dpage/pgadmin4" container_name: "" ports: - "5050:80" environment: PGADMIN_DEFAULT_EMAIL: "" PGADMIN_DEFAULT_PASSWORD: "123"

    Allow the pgAdmin service port in Ubuntu firewall on Docker Host.

    ahmer@docker-01:~/postgres-01$ sudo ufw allow 5050/tcp [sudo] password for ahmer: Rule added Rule added (v6)

    Start PostgreSQL and pgAdmin4 Docker containers using docker-compose command.

    ahmer@docker-01:~/postgres-01$ sudo docker-compose up Starting ... done Starting ... done Attaching to, | | PostgreSQL Database directory appears to contain a database; Skipping initialization | | 2020-03-05 14:56:19.547 UTC [1] LOG: listening on IPv4 address "", port 5432 | 2020-03-05 14:56:19.547 UTC [1] LOG: listening on IPv6 address "::", port 5432 | 2020-03-05 14:56:19.566 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" | 2020-03-05 14:56:19.600 UTC [24] LOG: database system was shut down at 2020-03-05 14:49:21 UTC | 2020-03-05 14:56:19.622 UTC [1] LOG: database system is ready to accept connections | [2020-03-05 14:56:19 +0000] [1] [INFO] Starting gunicorn 19.9.0 | [2020-03-05 14:56:19 +0000] [1] [INFO] Listening at: http://[::]:80 (1) | [2020-03-05 14:56:19 +0000] [1] [INFO] Using worker: threads | [2020-03-05 14:56:19 +0000] [81] [INFO] Booting worker with pid: 81


    Testing PostgreSQL and pgAdmin Docker Containers:

    Open URL in a browser.


    Login as

    • User:
    • Password: 123


    Click on Add New Server to add a PostgreSQL database server.



    Provide connection settings as we have provided above and click on Save.


    Our PostgreSQL database server has been added in pgAdmin 4 docker container.

    We have successfully configure and started PostgreSQL and pgAdmin Docker containers.

    No comments:

    Post a comment