PostgreSQL Docker Compose Configuration
This guide provides a production-ready PostgreSQL configuration using Docker Compose.
Basic Configuration
yaml
version: '3.8'
services:
postgres:
image: postgres:15-alpine
container_name: postgres
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./postgres/init:/docker-entrypoint-initdb.d
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
networks:
app-network:
driver: bridge
Environment Variables
Create a .env
file:
env
POSTGRES_USER=your_user
POSTGRES_PASSWORD=your_password
POSTGRES_DB=your_database
Usage
- Save the configuration as
docker-compose.yml
- Create the
.env
file with your credentials - Run:
docker-compose up -d
Best Practices
- Use Alpine-based images for smaller footprint
- Configure proper PGDATA path
- Enable healthcheck
- Use volumes for data persistence
- Set up proper networking