A production-ready Docker setup for PostgreSQL 18.1 with comprehensive monitoring, performance optimization, and advanced extensions.
This project provides a complete Docker-based PostgreSQL 18.1 environment with:
- Advanced Performance Monitoring: Built-in pg_stat_statements, pg_stat_kcache, and POWA dashboard
- Automatic Memory Optimization: Intelligent configuration based on container resources
- Comprehensive Auditing: pgaudit extension for detailed logging
- High Availability Features: Streaming replication support
- Extended Functionality: 20+ PostgreSQL extensions pre-installed
- Production Hardening: Optimized configuration for high-concurrency environments
- Docker and Docker Compose
- At least 4GB RAM available (8GB recommended)
- 20GB+ disk space for PostgreSQL data
- Clone or download this repository:
cd docker-postgresql-aio- Create a
.envfile in the root directory:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_secure_password_here
POSTGRES_DB=maindb
PG_TARGET_PERCENT=70
PG_CONN_ESTIMATE_MB=25
PG_MAX_CONN_CAP=2000- Start the PostgreSQL container:
docker-compose up -d- Verify the service is running:
docker-compose ps
docker exec db pg_isready -U postgres| Variable | Default | Description |
|---|---|---|
POSTGRES_USER |
postgres | PostgreSQL superuser username |
POSTGRES_PASSWORD |
- | PostgreSQL superuser password (required) |
POSTGRES_DB |
postgres | Initial database to create |
PG_TARGET_PERCENT |
70 | Target percentage of memory for shared_buffers |
PG_CONN_ESTIMATE_MB |
25 | Estimated memory per connection (MB) |
PG_MAX_CONN_CAP |
2000 | Maximum number of connections |
Edit config/postgresql.conf to customize:
- Connection limits (max_connections: 2000)
- Memory allocation (shared_buffers: 8GB, effective_cache_size: 24GB)
- WAL configuration for replication
- Logging verbosity and format
- Performance parameters
Edit config/pg_hba.conf to configure:
- Network access permissions
- Authentication methods
- User/database restrictions
Custom SQL scripts in docker/initdb/ run automatically on first startup:
- 01_init_dba.sh - Creates extensions and POWA repository database
- Adaptive Configuration: Automatically adjusts settings based on available CPU and memory
- Connection Pooling Ready: Optimized for use with pgBouncer
- Parallel Query Execution: max_parallel_workers set to 16
- Query Caching: Efficient buffer management with huge_pages support
Preloaded Extensions:
pg_stat_statements: Track query performance and execution statisticspg_stat_kcache: Measure kernel cache performanceauto_explain: Automatic query plan loggingpg_wait_sampling: Wait event analysispgaudit: Comprehensive audit loggingPOWA: PostgreSQL Workload Analyzer
Logging Features:
- All SQL statements logged with duration
- Connection/disconnection tracking
- Lock wait monitoring
- Detailed error reporting with verbose output
- pgaudit Extension: Complete audit trail of all database operations
- SCRAM-SHA-256: Modern password encryption
- Flexible Authentication: Configurable via pg_hba.conf
Preinstalled extensions include:
uuid-ossp: UUID generationpgcrypto: Cryptographic functionspglogical: Logical replicationtimescaledb: Time-series data handlingpg_hint_plan: Query optimization hintspg_partman: Automated table partitioningpgautofailover: Automatic failover management- And 10+ more specialized extensions
docker-postgresql-aio/
├── docker-compose.yml # Service orchestration
├── config/
│ ├── postgresql.conf # PostgreSQL main configuration
│ └── pg_hba.conf # Host-based authentication
├── docker/
│ ├── Dockerfile # Container image definition
│ ├── pg-entrypoint.sh # Startup script with auto-tuning
│ └── initdb/
│ └── 01_init_dba.sh # Initial database setup
└── README.md # This file
- shared_buffers: 8GB (efficient for large datasets)
- effective_cache_size: 24GB (OS cache estimate)
- work_mem: 8MB per operation
- maintenance_work_mem: 1GB (for VACUUM, CREATE INDEX)
- Effective I/O concurrency: 300 (SSD optimized)
- Parallel workers: Up to 16 parallel queries
- WAL compression: Enabled for efficient replication
- Max connections: 2000
- Reserved superuser connections: 10
- Autovacuum workers: 8
Check logs:
docker-compose logs dbVerify environment variables in .env file.
Reduce container memory limits or adjust PG_TARGET_PERCENT in .env.
Ensure the container is running:
docker-compose psCheck if port 5432 is available on the host.
Enable query monitoring:
SELECT * FROM pg_stat_statements
ORDER BY total_time DESC LIMIT 10;The container includes a health check that verifies PostgreSQL is accepting connections:
docker-compose ps # Shows health status- Backup your data
- Update PostgreSQL version in
Dockerfile - Rebuild and redeploy:
docker-compose down
docker-compose build --no-cache
docker-compose up -dLogs rotate automatically every 10 minutes. View logs:
docker exec db tail -f /var/log/postgresql/main.logCheck active extensions:
docker exec db psql -U postgres -c "SELECT * FROM pg_extension;"The configuration includes WAL settings for replica setup:
wal_level = replica
max_wal_senders = 10
wal_keep_size = 8GB
This setup works well with external poolers like pgBouncer:
pgBouncer (external) → Docker PostgreSQL → Applications
Default timezone is set to Europe/Istanbul. Change in:
docker-compose.yml:TZenvironment variableconfig/postgresql.conf:timezoneparameter
- For more connections: Increase
max_connectionsin postgresql.conf - For faster writes: Adjust
wal_compressionandsynchronous_commit - For heavy analytics: Increase
max_parallel_workersandwork_mem - For OLTP workloads: Reduce
shared_buffersand increase connection pool
This is a production-ready PostgreSQL setup maintained for Docker-based environments.
For issues or improvements, please review the configuration files and PostgreSQL documentation.
Version: PostgreSQL 18.1
Docker Image: akinbicer/postgres:18.1
Please use the Issue > New Issue button to submit issues, feature requests or support issues directly to me. You can also send an e-mail to akin.bicer@outlook.com.tr.