Share on Social Media

In this guide, you will learn how to install PostgreSQL in Docker Containers. #centlinux #postgres #docker

What is PostgreSQL?

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 based Linux 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 (PAID LINK) by Manning Publications before moving forward with this article.

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 on Red Hat based Linux server, please follow our previous article to install Docker on CentOS.

Install PostgreSQL in 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.

$ mkdir ~/postgres-01
$ cd postgres-01

Create a directory for PostgreSQL data files.

$ mkdir postgres_data

Create a docker-compose.yml file.

$ vi docker-compose.yml

And define postgres service therein.

version: "3.1"
    image: "postgres:11"
    container_name: ""
      - "5432:5432"
      - ./postgres_data:/var/lib/postgresql/data
   - "pgadmin"

Pull the required postgres image from Docker Hub.

$ sudo docker image pull postgres:11
11: Pulling from library/postgres
Status: Downloaded newer image for postgres:11

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

$ sudo docker-compose up
[sudo] password for ahmer:
Creating ... done
Attaching to
... | 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.

$ 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
root@7bb0d4f1e4a6:/# exit

PostgreSQL docker container is successfully configured.

Install pgAdmin4 in Docker Container:

Pull the pgAdmin4 image from Docker Hub.

$ sudo docker pull dpage/pgadmin4
Using default tag: latest
latest: Pulling from dpage/pgadmin4
Status: Downloaded newer image for dpage/pgadmin4:latest

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

$ vi docker-compose.yml

Now define pgadmin service under the services section.

    image: "dpage/pgadmin4"
    container_name: ""
      - "5050:80"

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

$ sudo ufw allow 5050/tcp
[sudo] password for ahmer:
Rule added
Rule added (v6)

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

$ sudo docker-compose up
Starting ... done
Starting ... done
Attaching to,
... | [2020-03-05 14:56:19 +0000] [81] [INFO] Booting worker with pid: 81

Test PostgreSQL and pgAdmin Containers:

Open URL in a web browser.

pgAdmin 4 Login

Login as

  • User:
  • Password: 123
postgresql dashboard

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

pgAdmin 4 Create Server
pgadmin4 add new server

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

pgadmin4 dashboard

Our PostgreSQL database server has been added in pgAdmin4 docker container.


In this article, you have learned how to install PostgreSQL in Docker containers.

Leave a Reply

Your email address will not be published. Required fields are marked *