Trench Project / Project Description / Code / Connect Docker with Postgres
Trench Project / Connect Docker with Postgres
Author: Simon Pesut
This guide explains how to set up a PostgreSQL database and pgAdmin using Docker. It also demonstrates how to connect pgAdmin to the database step-by-step.
1. Prerequisites
Before proceeding, ensure you have the following installed and ready:
-
Docker Desktop (running in Linux container mode).
-
Docker Compose.
-
Basic familiarity with Docker commands.
2. Step 1: Docker Compose Configuration
-
Create a
docker-compose.yml
file with the following content:
services:
postgres:
image: postgres
container_name: postgres_container
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: your_password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin_container
environment:
PGADMIN_DEFAULT_EMAIL: your_email@example.com
PGADMIN_DEFAULT_PASSWORD: your_password
ports:
- "8080:80"
volumes:
postgres_data:
This file defines two services:
-
postgres
: Runs the PostgreSQL database. -
pgadmin
: Runs the pgAdmin web-based database management interface.
3. Step 2: Start the Services
Run the following commands to start the containers:
$ docker-compose up -d
Once the containers are up, confirm that they’re running:
$ docker ps
This command will display a list of running containers. Ensure both postgres_container
and pgadmin_container
are listed.
4. Step 3: Access the Services
4.1. Access pgAdmin
-
Open your web browser and go to:
http://localhost:8080
-
Log in using the credentials defined in the
docker-compose.yml
file:-
Email:
your_email@example.com
-
Password:
your_password
-
4.2. Connect pgAdmin to PostgreSQL
-
In pgAdmin, right-click on Servers and select Create → Server.
-
Fill in the details on the following tabs:
[.table-striped,.table-bordered,.table-hover] |=== | **Tab** | **Field** | **Value** | General | Name | My PostgreSQL Server | Connection | Host | localhost or host.docker.internal | Connection | Port | 5432 | Connection | Maintenance DB | postgres | Connection | Username | postgres | Connection | Password | your_password |===
-
Click Save. pgAdmin will now connect to your PostgreSQL database.
5. Common Issues and Troubleshooting
5.1. Docker Desktop is not running
Ensure Docker Desktop is fully started. Open it and wait until the status icon in the taskbar indicates it is running.
5.2. Linux Container Mode
If Docker Desktop is not in Linux Container mode:
-
Right-click the Docker Desktop icon in the taskbar and select Switch to Linux Containers.