How postgres is used with docker
- Postgres Docker Container
- Postgres - Actual Postgres instance running inside the container.
- Psql - Powerful and lightweight command-line tool included in standard PostgreSQL distribution, therefore, available within the Postgres Docker Container
- We will use psql from within the container to connect to the database and run sql commands.
- Terminal - Runs on host OS.
- We will use this to spin up thePostgres container and then connect to the database using the psql tool.
Linux (Fedora)
Step 1: Create a volume for persistent storage
bash
sudo docker volume create postgres-volume- Where is it stored?
sudo docker volume inspect postgres-volume[
{
"CreatedAt": "2026-02-26T11:34:19+05:30",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/postgres-volume/_data",
"Name": "postgres-volume",
"Options": null,
"Scope": "local"
}
]Step 2: Start a postgres container
- Start a db container named Postgres
- Environment Variables - Username and Password
- Maps container’s port to host’s port
- Mounts the volume
- Starts the container as a background process
bash
sudo docker run --name postgres \
-e POSTGRES_USER=abcd \
-e POSTGRES_PASSWORD=1234 \
-p 5432:5432 \
-v postgres-volume:/var/lib/postgresql/data \
-d postgres:17.2Question : Does Postgres have a default user/password?
- Default User
postgres - Default Password : No
- We must one provide via:
-e POSTGRES_PASSWORD=secret. If we don’t, the container will refuse to start (unless we explicitly allow trust auth, which is insecure).
- We must one provide via:
Question: What does POSTGRES_PASSWORD actually do?
bash
sudo docker run --name postgres \
-e POSTGRES_USER=abcd \
-e POSTGRES_PASSWORD=1234 \
-p 5432:5432 \
-v postgres-volume:/var/lib/postgresql/data \
-d postgres:17.2This does:
- Creates a database cluster (if volume is empty)
- Creates a superuser named
abcd- The default
postgresrole is NOT created unless you explicitly create it - If username is not provided then
postgresbecomes the default DB
- The default
- Sets its password to
1234
It is a database superuser. It can:
- Create databases
- Create roles
- Drop tables
- Bypass permissions
- Read/write any DB data
But it cannot:
- Access host system files
- Become system root
- Escape Docker
Question: What if Docker isn’t installed yet?
bash
sudo dnf install docker
sudo systemctl start docker
sudo systemctl enable docker # auto-start on boot- Check it’s working:
bash
sudo docker --version
sudo docker psQuestion: What if the container fails to start?
- Check what went wrong:
bash
sudo docker logs postgresCommon reasons:
- Port 5432 already in use (maybe a local postgres is running) → stop it:
sudo systemctl stop postgresql - Volume already has data from a different postgres version → wipe the volume or use a matching image version
- Name conflict → a container named
postgresalready exists (stopped) →sudo docker rm postgres
Question: How do I check if the container is running?
bash
sudo docker ps # running containers
sudo docker ps -a # all containers (including stopped)Question: How do I connect to the running postgres?
Option 1 - from inside the container:
bash
sudo docker exec -it postgres psql -U abcdOption 2 - from host machine (if psql is installed on host):
bash
psql -h localhost -p 5432 -U abcdOption 3 - from another container on the same machine, use the container name as host:
bash
psql -h postgres -p 5432 -U abcd(This only works if both containers are on the same Docker network)
Question: What if I forget the password?
- You can’t recover it easily. Best option is to wipe and recreate:
bash
sudo docker rm -f postgres
sudo docker volume rm postgres-volume
# then re-run with new credentials- OR connect without password using trust auth temporarily (not recommended for production).
Using this container from the next time
STEP 1: Start/ Stop the same container
- Start
bash
sudo docker stop postgres- Stop
bash
sudo docker start postgresThis uses the same container and the same mounted volume, so everything persists.
Question: Container stopped on its own / after reboot?
- Docker containers don’t auto-restart after a system reboot by default.
- Fix: add
--restart unless-stoppedwhen creating the container:
bash
sudo docker run --name postgres \
--restart unless-stopped \
-e POSTGRES_USER=abcd \
-e POSTGRES_PASSWORD=1234 \
-p 5432:5432 \
-v postgres-volume:/var/lib/postgresql/data \
-d postgres:17.2- Or for an already existing container:
bash
sudo docker update --restart unless-stopped postgres- Also make sure Docker itself starts on boot:
bash
sudo systemctl enable dockerUsing new container from next time
If we remove the container, the volume stays (unless we explicitly remove it).
- Remove container (data safe because it’s in the volume):
bash
sudo docker rm -f postgres- Create a new container later reusing the same volume:
bash
sudo docker run --name postgres \
-e POSTGRES_USER=abcd \
-e POSTGRES_PASSWORD=1234 \
-p 5432:5432 \
-v postgres-volume:/var/lib/postgresql/data \
-d postgres:17.2Those POSTGRES_* env vars will be ignored if the volume already has a cluster. They’re basically “documentation” at that point, not configuration.
Question: What if I reuse a volume but change the postgres image version?
- Postgres does NOT auto-upgrade data between major versions (e.g. 15 → 17).
- The container will just fail to start and show an error in logs.
- You need to either stick to the same major version, or do a proper
pg_dump→ restore.
Complete Fresh Start
- If you want a completely new DB cluster (wipe everything):
bash
sudo docker rm -f postgres
sudo docker volume rm postgres-volumeQuestion: How do I take a backup before wiping?
bash
sudo docker exec -t postgres pg_dumpall -U abcd > backup.sql- Restore later:
bash
cat backup.sql | sudo docker exec -i postgres psql -U abcdUse Docker Compose
yaml
services:
postgres:
image: postgres:17.2
container_name: postgres
restart: unless-stopped
ports:
- "5432:5432"
environment:
POSTGRES_USER: abcd
POSTGRES_PASSWORD: "1234"
volumes:
- postgres-volume:/var/lib/postgresql/data
volumes:
postgres-volume:Start : sudo docker compose up -d
Stop: sudo docker compose down
Start again later : sudo docker compose up -d
The volume persists across down/up unless you add -v to down.
Wipe everything including volume:
bash
sudo docker compose down -vQuestion: How to see container resource usage?
bash
sudo docker stats postgresQuestion: How to list all volumes and clean up unused ones?
bash
sudo docker volume ls
sudo docker volume prune # removes all volumes not attached to any containerBe careful with prune — it deletes data.