Overview
This guide shows how to run PostgreSQL 16 locally using the official Docker image and Docker Compose. You get a default database user and database, port 5432 on your machine mapped into the container, and a named volume so data survives container restarts.
Prerequisites
- Docker Engine installed (Get Docker).
- Docker Compose v2 (bundled with Docker Desktop; on Linux you may use the
docker composeplugin).
The version: key at the top of a Compose file is optional in modern Compose; including version: '3.8' is still fine for compatibility.
Create docker-compose.yml
Create a file named docker-compose.yml in your project directory:
| |
What this does:
image: postgres:16— Official PostgreSQL 16 image.restart: always— Restarts the container if it exits or after a host reboot (when Docker starts).environment— Creates the default role and database on first run.ports— Maps host5432to the container. If something else already uses5432on your machine, change the left side (e.g."5433:5432") and connect to that host port instead.volumes—postgres_datais a Docker named volume; it stores data at/var/lib/postgresql/datainside the container (the correct path for the official image).
Security: For real projects, avoid committing real passwords in shared repositories. Prefer environment files (e.g. .env not committed) with env_file: or your orchestrator’s secrets, and use strong passwords.
Run and verify
From the directory that contains docker-compose.yml:
| |
Check logs if needed:
| |
You should see PostgreSQL ready to accept connections once startup finishes.
Connect
From the host, using the same user, password, database, and host port you configured (default 5432):
| |
DBeaver
- Open DBeaver and choose Database → New Database Connection (or click New Connection in the toolbar).
- Select PostgreSQL and click Next.
- On the Main tab, set:
- Host:
localhost - Port:
5432(or the host port you mapped indocker-compose.yml, e.g.5433if you used"5433:5432") - Database:
my_database - Username:
my_user - Password:
my_super_secret_password(check Save password if you want it stored locally)
- Host:
- Click Test Connection. If prompted, let DBeaver download the PostgreSQL JDBC driver the first time.
- Click Finish to save the connection.
For a typical local Docker setup, leave SSL off (or disable) on the SSL tab unless you have configured TLS yourself.
Other GUI tools
Use the same values as above:
- Host:
localhost - Port:
5432(or the host port you mapped) - Database:
my_database - User / password: as in
POSTGRES_USER/POSTGRES_PASSWORD
Connection URI form:
| |
Useful commands
Stop containers (keeps the named volume and data):
| |
Stop and remove containers (volume still keeps data unless you remove it):
| |
Remove the named volume and delete persisted data (use only when you intend to wipe the database):
| |
List volumes:
| |
Summary
You now have PostgreSQL 16 running in Docker with persistent storage via postgres_data, accessible on localhost at the mapped port. Adjust credentials and ports for your environment, and keep secrets out of version control for anything beyond local experimentation.
