Latest

Thursday, 5 March 2020

Configure PostgreSQL and pgAdmin Docker Containers

configure-postgresql-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 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 by Manning Publications before moving forward with this article.

 

Table of Contents:

configure-postgresql-pgadmin-docker-containers

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 – docker-01.centlinux.com
  • IP Address - 192.168.116.218 /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.

 

Configure PostgreSQL Docker Container:

Connect with docker-01.centlinux.com 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"
services:
  db:
    image: "postgres:11"
    container_name: "postgres-01.centlinux.com"
    ports:
      - "5432:5432"
 environment:
      POSTGRES_PASSWORD: "123"
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
 links:
   - "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 postgres-01.centlinux.com ... done
Attaching to postgres-01.centlinux.com
...
postgres-01.centlinux.com | 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 postgres-01.centlinux.com 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 pgAdmin4 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.

 pgadmin:
    image: "dpage/pgadmin4"
    container_name: "pgadmin4.centlinux.com"
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: "ahmer@centlinux.com"
      PGADMIN_DEFAULT_PASSWORD: "123"

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 pgadmin4.centlinux.com ... done
Starting postgres-01.centlinux.com ... done
Attaching to pgadmin4.centlinux.com, postgres-01.centlinux.com
...
pgadmin4.centlinux.com | [2020-03-05 14:56:19 +0000] [81] [INFO] Booting worker with pid: 81

 

Testing PostgreSQL and pgAdmin Docker Containers:

Open URL http://docker-01.centlinux.com:5050 in a web browser.

01-postgresql-docker-pgadmin-login

Login as

  • User: ahmer@centlinux.com
  • Password: 123

02-postgresql-docker-pgadmin-dashboard

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

03-postgresql-docker-pgadmin-add-new-server

04-postgresql-docker-pgadmin-add-new-server

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

05-postgresql-docker-pgadmin-dashboard

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

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

If you find this article useful? Don't you mind support us by Buy Me A Coffee


No comments:

Post a Comment

© 2022 CentLinux. All Rights Reserved.