This document explains how to use Docker Compose to run a PostgreSQL database in a Docker container.
version: '3.8'
services:
postgres:
image: postgres:15-alpine
container_name: postgres_db
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- postgres_network
volumes:
postgres_data:
networks:
postgres_network:
driver: bridge
- postgres: PostgreSQL database service
image: postgres:15-alpine
- Uses PostgreSQL version 15 with lightweight Alpine Linux base imagecontainer_name: postgres_db
- Name of the container to be createdenvironment
- Environment variables for database configuration:POSTGRES_DB
- Default database namePOSTGRES_USER
- Username for database connectionPOSTGRES_PASSWORD
- Password for database user
"5432:5432"
- Maps container port 5432 to host port 5432
postgres_data
- Volume for database data persistence- Data is stored at
/var/lib/postgresql/data
within the container
postgres_network
- Bridge network for container connectivity
Ensure Docker and Docker Compose are installed on your system.
# Start services in detached mode (background)
docker-compose up -d
# Or start with logs visible
docker-compose up
# Check container status
docker-compose ps
# View logs
docker-compose logs postgres
# View real-time logs
docker-compose logs -f postgres
# Stop services
docker-compose down
# Stop and remove volumes (WARNING: data will be lost)
docker-compose down -v
# Restart services
docker-compose restart
# Stop services without removing containers
docker-compose stop
# Start previously stopped services
docker-compose start
# Access shell inside container
docker exec -it postgres_db bash
# Then connect to database
psql -U myuser -d mydatabase
# Ensure psql is installed on host system
psql -h localhost -p 5432 -U myuser -d mydatabase
- Host:
localhost
- Port:
5432
- Database:
mydatabase
- Username:
myuser
- Password:
mypassword
# Backup using docker exec
docker exec -t postgres_db pg_dump -U myuser mydatabase > backup.sql
# Restore from backup file
cat backup.sql | docker exec -i postgres_db psql -U myuser -d mydatabase
Variable | Description | Default |
---|---|---|
POSTGRES_DB | Default database | postgres |
POSTGRES_USER | Superuser username | postgres |
POSTGRES_PASSWORD | Superuser password | - |
POSTGRES_HOST_AUTH_METHOD | Authentication method | md5 |
PGDATA | Data directory location | /var/lib/postgresql/data |
- Change default passwords - Always use strong passwords
- Use custom networks - For better isolation
- Regular backups - Perform regular data backups
- Environment files - Use environment files to store credentials
Create .env
file:
POSTGRES_DB=mydatabase
POSTGRES_USER=myuser
POSTGRES_PASSWORD=strongpassword123
Update docker-compose.yml:
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
-
Port already in use
# Check processes using port 5432 netstat -tulpn | grep 5432 # Or change port in docker-compose.yml ports: - "5433:5432"
-
Permission error on volume
# Remove volume and recreate docker-compose down -v docker-compose up -d
-
Container won't start
# Check logs for error details docker-compose logs postgres
-
Development vs Production:
- Development: Use port mapping for easy access
- Production: Consider not exposing ports and use internal networking
-
Performance:
- Adjust resource limits based on requirements
- Use optimal volumes for I/O performance
-
Monitoring:
- Use
docker stats
to monitor resource usage - Set up appropriate logging for audit purposes
- Use
This documentation covers basic Docker Compose usage with PostgreSQL. Adjust configurations based on your specific application requirements.
services:
postgres:
# ... other configs
deploy:
resources:
limits:
memory: 1G
cpus: '1.0'
reservations:
memory: 512M
cpus: '0.5'
services:
postgres:
# ... other configs
healthcheck:
test: ["CMD-SHELL", "pg_isready -U myuser -d mydatabase"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
environment:
POSTGRES_MULTIPLE_DATABASES: "mydatabase,anotherdb,testdb"
This documentation provides comprehensive guidance for running PostgreSQL with Docker Compose in various environments.