docker-compose for PostgreSQL: example file and generator
Run a local PostgreSQL database that keeps its data.
This docker-compose file starts a PostgreSQL database on your computer with one command and keeps your data between restarts. Choose your version, user, password and port below, then run docker compose up -d.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
services:
db:
image: postgres:16
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: appdb
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:How this setup works
Step-by-step setup
- Install Docker Desktop (Windows, macOS) or Docker Engine (Linux).
- Save the config above as docker-compose.yml in an empty folder.
- Change the password.
- Run docker compose up -d.
- Connect: psql -h localhost -p 5432 -U app appdb.
What the important lines mean
- volumes keeps your data when the container is removed.
- postgres:16 pins the version so updates do not surprise you.
- "5432:5432" is computer port : container port.
Common errors and how to fix them
- port is already allocated
- Something else already uses port 5432. Pick another port above, such as 5433.
- password authentication failed
- The password is only set the first time. Reset with docker compose down -v (this deletes the data).
Frequently asked questions
- How do I connect to PostgreSQL running in Docker?
- Use host localhost, the port you chose (5432 by default), and the user, password and database from the file. For example: psql -h localhost -U app appdb.
- Will I lose my data when I stop the container?
- No, as long as the volume is in the file. Data is lost only if you run docker compose down -v, which deletes the volume.
- Why does my password not change when I edit the file?
- Postgres reads the password only the first time it creates the database. To start fresh, run docker compose down -v and then up again. This deletes the data.
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- Docker port already allocatedFix "port is already allocated" in Docker and Docker Compose: find the container or program that holds the port and free it.
- Docker container cannot reach localhostFix "connection refused" when an app in Docker calls localhost: use host.docker.internal, a Compose service name or host networking.
More container configs
- docker-compose for MySQLRun a local MySQL database in Docker with your own user, password and port, and keep the data between restarts.
- docker-compose for RedisRun Redis in Docker with a password, saved data and a memory limit.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.
- Dockerfile for Node.jsA small, working Dockerfile for a Node.js app, with build step, non-root user and health check options.
- Dockerfile for PythonA working Dockerfile for a Python web app, with Gunicorn, Uvicorn or a plain script.
- Docker Compose for MailpitRun a fake mail server on your computer, so test emails from your app show up in a web page instead of being sent.