Run PostgreSQL and PGAdmin using docker compose

Vishal Sharma
2 min readApr 20, 2023

--

Running PostgreSQL on native operating system is always a hassle. First we have to perform the installation on the respective operating system be it Linux, Windows or MacOS. The procedure of installation varies from OS to OS.

We will use Docker (docker compose precisely) to deploy PostgreSQL along with PGAdmin on the local environment.

Once you have installed docker on the local machine the setup will run despite of the type of operating system.

# Create a directory and docker-compose.yml file within the directory
mkdir postgres-docker
cd postgres-docker
vi docker-compose.yml

The content of the docker-compose.yml file as per follow:

version: "3.8"
services:
db:
image: postgres
container_name: local_pgdb
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: user-name
POSTGRES_PASSWORD: strong-password
volumes:
- local_pgdata:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin4_container
restart: always
ports:
- "8888:80"
environment:
PGADMIN_DEFAULT_EMAIL: user-name@domain-name.com
PGADMIN_DEFAULT_PASSWORD: strong-password
volumes:
- pgadmin-data:/var/lib/pgadmin

volumes:
local_pgdata:
pgadmin-data:

You can modify following values if you wish to:

services:
db:
ports:
- "5432:5432"
environment:
POSTGRES_USER: user-name
POSTGRES_PASSWORD: strong-password
pgadmin:
ports:
- "8888:80"
environment:
PGADMIN_DEFAULT_EMAIL: user-name@domain-name.com
PGADMIN_DEFAULT_PASSWORD: strong-password

Run the below command to run PostgreSQL and PGAdmin in the detached mode

docker-compose up -d

# On latest docker version you might need to run

docker compose up -d

Check the docker container running status

Now the use PGAdmin tool, open the browser and access http://localhost:8888/. Enter the username and password for PGAdmin.

Now, connect to the PostgreSQL Database using DB credentials.

It should connect with PostgreSQL DB.

Thank you …

--

--

Vishal Sharma

IT Enthusiast, Professional, Novice Sportsperson and Fantasist of Better World. @byVishalSharma on Twitter.