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.
- The first start creates user app and database appdb. Later changes to these variables are ignored while the volume exists.
- Any .sql or .sh file you mount into /docker-entrypoint-initdb.d runs once, on the first start only.
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).
- Database is uninitialized and superuser password is not specified
- POSTGRES_PASSWORD is the one variable the image requires, and it must not be empty. Check the line in the file is not blank.
- Changed POSTGRES_USER or POSTGRES_DB but nothing changed
- These variables, like the password, only count when the data folder is empty. For a new database in an existing volume, connect and run CREATE DATABASE name;, or reset with docker compose down -v.
- could not resize shared memory segment / No space left on device
- Containers get only 64MB of shared memory by default. Add shm_size: 256mb under the db service for heavy queries.
- Data is gone after changing the volume path
- On Postgres 17 and older the volume must be mounted at /var/lib/postgresql/data, as in this file. Mounting one level up does not keep 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.
- How do I run a SQL file when the database starts?
- Add a line under volumes such as ./init.sql:/docker-entrypoint-initdb.d/init.sql. The image runs it only when the data directory is empty, so edits later need docker compose down -v first.
- Why does my app in another container fail to connect to localhost?
- Inside a container, localhost is that container. Other services in the same compose file reach the database at host db (the service name) on port 5432, not the port on your computer.
- Should I use POSTGRES_HOST_AUTH_METHOD=trust?
- The image documentation advises against it, because it allows connections without a password. Keep the password set, as this file does.
Free tools that help
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 port mappingWhat -p 8080:80 and ports: "8080:80" mean in Docker: the first number is on your computer, the second is inside the container. Which URL to open and how to fix 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.