All tools / Docker Compose examples
PostgreSQL with Docker Compose
PostgreSQL with a named volume, a health check and the port published only on localhost.
Customize names, versions and passwords →
compose.yaml
services:
postgres:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: "app"
POSTGRES_PASSWORD: "change-me"
POSTGRES_DB: "app"
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "127.0.0.1:5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres-data:
Passwords here are placeholders (change-me). The generator creates random ones for you.
How to use it
Commands
docker compose up -d docker compose ps docker compose logs -f
- Connect with postgresql://app:<password>@localhost:5432/app. Other services in the same file use the host postgres.
- For postgres 18 and later images, mount the volume at /var/lib/postgresql instead of /var/lib/postgresql/data.
Prefer Kubernetes? See the PostgreSQL Kubernetes version.