Using Docker Compose to set up PostgreSQL in a development environment greatly simplifies version management and portability.
docker-compose.yml
version: '3.8'
services:
postgres:
image: postgres:16-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: myapp
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Advantages
- Different PostgreSQL versions for different projects
- Start/stop with a single command
- Data persistence with volumes
Connection
While the container is running, you can connect via localhost:5432.