When to use this stack
PostgreSQL is a general-purpose relational database with strong SQL standards support, real transactions, JSONB columns and a large extension ecosystem. This stack gives you one PostgreSQL 16 server with a healthcheck, data stored in a named Docker volume, and pgAdmin 4 as a web client for schema work, ad hoc queries and manual backups. It covers the common case of a small team or a single VPS that needs a dependable database and a graphical tool, without installing either directly on the host operating system.
Use this stack when your application runs in the same Compose project or on the same Docker network, when you want a local development database that matches the major version you run in production, or when you need a browser client for occasional administration. Do not treat it as a high availability setup. Failover across machines, streaming replicas and point-in-time recovery need extra configuration such as replication and a backup tool like pgBackRest, or a managed service. Also do not publish port 5432 to the internet. This stack keeps the database private on purpose and exposes only the admin UI, and only on localhost.
docker-compose.yml
services:
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 10s
timeout: 5s
retries: 5
start_period: 20s
pgadmin:
image: dpage/pgadmin4:8.12
restart: unless-stopped
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
ports:
- "127.0.0.1:5050:80"
volumes:
- pgadmin_data:/var/lib/pgadmin
depends_on:
postgres:
condition: service_healthy
volumes:
postgres_data:
pgadmin_data:# Database credentials used on the first start of the postgres volume
POSTGRES_USER=app_user
POSTGRES_PASSWORD=CHANGE_ME_STRONG_DB_PASSWORD
POSTGRES_DB=app_db
# Login for the pgAdmin web interface (must be an email address)
PGADMIN_DEFAULT_EMAIL=[email protected]
PGADMIN_DEFAULT_PASSWORD=CHANGE_ME_STRONG_PGADMIN_PASSWORD.env
# Database credentials used on the first start of the postgres volume
POSTGRES_USER=app_user
POSTGRES_PASSWORD=CHANGE_ME_STRONG_DB_PASSWORD
POSTGRES_DB=app_db
# Login for the pgAdmin web interface (must be an email address)
PGADMIN_DEFAULT_EMAIL=[email protected]
PGADMIN_DEFAULT_PASSWORD=CHANGE_ME_STRONG_PGADMIN_PASSWORDStart and verify
Load the variables into your shell so the checks below can use them. Compose reads .env on its own; the sourcing only serves the verification commands. The postgres service should show as healthy within about 30 seconds of starting.
set -a; . ./.env; set +a
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=50 postgres
docker compose exec postgres pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"
curl -s -o /dev/null -w "pgAdmin HTTP status: %{http_code}\n" http://127.0.0.1:5050/misc/pingProduction checklist
- Replace every CHANGE_ME value with a long random password, and never reuse the sample values.
- Keep port 5432 unpublished. Applications connect over the Compose network using the host name postgres.
- Keep pgAdmin on 127.0.0.1 and reach it through an SSH tunnel, or put it behind a TLS reverse proxy with its own access control.
- Create one database role per application with rights limited to its own schema, and keep the owner role for migrations only.
- Schedule nightly pg_dump backups to a different disk or remote storage, and test a restore at least once a month.
- Set a memory limit on the container and tune shared_buffers to a sensible share of the host RAM.
- Keep the image on the 16.x line and rebuild regularly to receive minor security fixes.
- Watch disk usage and WAL growth, because a full disk stops PostgreSQL from writing.
Common problems
-
Symptom: password authentication fails after you change POSTGRES_PASSWORD in .env. Cause: the variables are used only when the data directory is first created. Fix: run ALTER USER with the new password inside the running database, or delete the volume with docker compose down -v if the data is disposable.
-
Symptom: pgAdmin reports that it cannot connect when you register the server with host localhost. Cause: inside the pgAdmin container, localhost points to pgAdmin itself. Fix: register the server with host name postgres, port 5432, and the credentials from .env.
-
Symptom: the browser cannot reach http://your-server:5050. Cause: the port is bound to 127.0.0.1 on purpose. Fix: open an SSH tunnel to the server, or add a TLS reverse proxy on the host. Do not change the binding to 0.0.0.0 without access control.
-
Symptom: pgAdmin logs permission denied on its data directory after you switch to a bind mount. Cause: the pgAdmin process runs as UID 5050, which does not own the host directory. Fix: keep the named volume, or change the owner of the host directory to UID 5050.
Variations
- Different major version: change the tag and perform a dump and restore. Data directories from different major versions are not compatible.
- Connection pooling: add a connection pooler such as PgBouncer in front of postgres when many short-lived clients connect at once.
- Reverse proxy for pgAdmin: place Caddy or Nginx in front of pgAdmin with HTTPS, and expose only the proxy ports 80 and 443 to the internet.
Questions
Why is PostgreSQL not published to the host?
Applications on the same Compose network reach the database as postgres:5432, so the port does not need to be exposed. Publishing it would let anyone who can reach the host attempt logins against your database.
Why does a new POSTGRES_PASSWORD not work after I edit .env?
The official image reads POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB only when the data directory is empty. After the first start, change the password with ALTER USER inside the database, or remove the volume if its data is disposable.
How do I open pgAdmin from my own computer?
The port is bound to 127.0.0.1, so use an SSH tunnel such as ssh -L 5050:127.0.0.1:5050 user@your-server, then open http://localhost:5050 in your browser.
Can I upgrade from PostgreSQL 16 to 17 by changing the tag?
No. Major versions use incompatible data files. Dump the database with pg_dump, start the new major version on an empty volume and restore the dump into it.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
ClickHouse 25.8 Analytics Database
Docker Compose stack for ClickHouse 25.8 with a dedicated user, a healthcheck, raised file limits and HTTP and native ports on localhost for analytical queries.
Elasticsearch 8 + Kibana Search Stack
Docker Compose stack for Elasticsearch 8.19 and Kibana 8.19 with security enabled, a persistent volume, a healthcheck and localhost-only ports.
FastAPI + MongoDB 7 + Mongo Express
Docker Compose stack for a Python FastAPI API on MongoDB 7 with authentication, a healthcheck, persistent storage and a protected Mongo Express UI on localhost.
MariaDB 11.4 LTS + Adminer 5
Docker Compose stack with MariaDB 11.4 LTS, a healthcheck, a named volume and Adminer 5 on localhost. A lightweight admin UI for MySQL-compatible databases.
Browse more Databases & Search stacks or the full library.