This project provides a Docker Compose setup for deploying PostgreSQL with external access.
- PostgreSQL 15 with external port access
- Persistent data storage
- Health checks
- Environment variable configuration
- Custom initialization scripts support
-
Copy environment file:
cp env.example .env
-
Edit environment variables (optional):
nano .env
-
Start the database:
docker-compose up -d
-
Check status:
docker-compose ps
- Host: localhost (or your server IP)
- Port: 5432 (or custom port from .env)
- Database: postgres (or custom from .env)
- Username: postgres (or custom from .env)
- Password: postgres (or custom from .env)
The PostgreSQL container is configured to be accessible from outside Docker:
- Port 5432 is mapped to the host
- Network is configured as bridge for external connectivity
- No firewall restrictions within Docker
# Start services
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs postgres
# Access PostgreSQL shell
docker-compose exec postgres psql -U postgres
# Backup database
docker-compose exec postgres pg_dump -U postgres postgres > backup.sql
# Restore database
docker-compose exec -T postgres psql -U postgres postgres < backup.sql
- Database data is stored in a named volume
postgres_data
- Data persists between container restarts
- To completely reset:
docker-compose down -v
- Update
.env
file with strong passwords - Consider using Docker secrets for production
- Restrict network access as needed
Place SQL files in ./init-scripts/
directory - they will run on first startup.
Modify the image: postgres:15
line in docker-compose.yml
Extend the compose file with other services as needed.
- Check if container is running:
docker-compose ps
- Verify port mapping:
docker-compose port postgres 5432
- Check logs:
docker-compose logs postgres
- Ensure proper file permissions on mounted volumes
- Check PostgreSQL user configuration in environment variables