Database service configuration
The database is defined indocker-compose.yml:
docker-compose.yml:3-14
Key components
PostgreSQL 16 using the lightweight Alpine Linux baseVersion 16 features:
- Improved query performance
- Better index management
- Enhanced monitoring capabilities
Persistent storage for database filesAll database data is stored in
./db_data on the host, ensuring data survives container restarts and removals.Environment variables
Database initialization is controlled by PostgreSQL’s standard environment variables:Name of the database created during first initializationSet via
DB_NAME in deploy.shDatabase superuser usernameSet via
DB_USER in deploy.shDatabase superuser passwordSet via
DB_PASS in deploy.shDatabase connection from Moodle
The application container connects to PostgreSQL using these environment variables:docker-compose.yml:22-26
- MOODLE_DB_HOST: db — Docker service discovery resolves
dbto the database container’s IP - MOODLE_DB_TYPE: pgsql — Moodle’s PostgreSQL driver
Accessing the database
Using psql CLI
Connect to PostgreSQL from the host:Database backup
Create a full database dump:Database restore
Restore from a backup file:Performance optimization
PostgreSQL configuration
For production deployments, customize PostgreSQL settings. Createpostgres.conf:
postgres.conf
docker-compose.yml
Memory calculation guidelines
shared_buffers
25% of total RAM dedicated to the database
- 4GB RAM:
shared_buffers = 1GB - 8GB RAM:
shared_buffers = 2GB
effective_cache_size
50-75% of total RAM for disk cache
- 4GB RAM:
effective_cache_size = 2GB - 8GB RAM:
effective_cache_size = 4GB
work_mem
Total RAM / max_connections / 4
- 4GB RAM, 100 connections:
work_mem = 10MB - 8GB RAM, 200 connections:
work_mem = 10MB
maintenance_work_mem
5-10% of total RAM for maintenance ops
- 4GB RAM:
maintenance_work_mem = 256MB - 8GB RAM:
maintenance_work_mem = 512MB
Index optimization
Moodle creates necessary indexes, but you can add custom indexes for performance:VACUUM and ANALYZE
PostgreSQL needs regular maintenance to reclaim space and update statistics:postgres.conf:
Monitoring
Connection statistics
Database size monitoring
Slow query log
Enable slow query logging by adding topostgres.conf:
Security
Change default credentials
Restrict network access
The database is only accessible within the Docker network by default. To connect from outside (for backups or admin tools):docker-compose.yml
Enable SSL connections
For encrypted client-server communication, generate certificates and configure PostgreSQL:postgres.conf:
Troubleshooting
Database connection refused
Database connection refused
- Check database container is running:
docker compose ps db - Check logs:
docker compose logs db - Verify network connectivity:
docker compose exec app ping db - Ensure credentials match between
.envand Moodle’sconfig.php
Out of disk space
Out of disk space
Check database size:Check host disk:Clean up if needed:
Slow queries
Slow queries
- Enable query logging (see Monitoring section)
- Identify slow queries in logs
- Use
EXPLAIN ANALYZEto understand query plans:
- Add indexes for frequently queried columns
- Run
ANALYZEto update statistics
Database won't start after host reboot
Database won't start after host reboot
PostgreSQL may not shut down cleanly. Remove the PID file:
Backup strategies
See the Backup & Restore guide for comprehensive backup procedures including:- Automated daily backups
- Offsite backup replication
- Disaster recovery procedures
Next steps
Backup & Restore
Set up automated database backups
Monitoring
Monitor database performance
Security hardening
Additional database security measures
Scaling
Database replication and scaling