Using PostgreSQL on Docker

1. Prerequities

OS: CentOS7
PostgreSQL: PostgreSQL13
Docker version 20.10

2. Setup Docker

(1) Install docker engine performing the following commands

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum makecache fast
sudo yum install docker-ce
systemctl enable docker

(2) Check whether docker engine runs sample container or not

docker run hello-world
docker stop hello-world

3. Using PostgreSQL from Docker

(1) Run PostgreSQL from docker engine

docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 15432:5432 -v docker run -d --name postgres13-server1 -e POSTGRES_PASSWORD=postgres -p 15432:5432 -v /tmp/pgsql13/data:/var/lib/postgresql/data postgres:13-alpine

(2) Check whether PostgreSQL is running or not

docker ps

(3) If PostgreSQL is running then the container information such as the following is shown

CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                     NAMES
c78000879d49   postgres:13-alpine   "docker-entrypoint.s…"   9 seconds ago   Up 8 seconds   0.0.0.0:15432->5432/tcp   postgres13-server1

(3) Connect to PostgreSQL

psql -U postgres -h 0.0.0.0 -p 15432

(4) Stop PostgreSQL container.

docker stop postgres13-server1

4. References

[1] Install Docker Engine on CentOS
https://docs.docker.com/engine/install/centos/

[2] PostgreSQL Docker Official Images
https://hub.docker.com/_/postgres?tab=tags&page=1&ordering=last_updated

Published by ktke109

I love open souce database management systems.