PostgreSQL is the primary relational database for the Django application, storing all application data including user information, todo items, and other models. It provides ACID-compliant transactions, robust data integrity, and excellent performance for web applications.
Description
PostgreSQL is a powerful, open-source object-relational database system with over 35 years of active development. In this stack, it:
Stores Django application data (models, users, sessions)
# PostgreSQL Databaseobs-postgres:image:postgres:16-alpine# PostgreSQL 16 on Alpine (smaller image)container_name:obs-postgresrestart:unless-stoppedports:-"5439:5432"# Expose on host port 5439environment:POSTGRES_DB:${DB_NAME}# Database namePOSTGRES_USER:${DB_USER}# Database usernamePOSTGRES_PASSWORD:${DB_PASSWORD}# Database passwordvolumes:-postgres_data:/var/lib/postgresql/data# Persistent database storagehealthcheck:test:["CMD-SHELL","pg_isready-U${DB_USER}-d${DB_NAME}"]interval:10s# Check every 10 secondstimeout:5s# Wait 5 seconds for responseretries:5# Try 5 times before failingstart_period:10s# Wait 10s before first checknetworks:-app_network# Django can connect here
pgAdmin Service
# pgAdmin Web Interfaceobs-pgadmin:image:dpage/pgadmin4:latest# pgAdmin on Alpinecontainer_name:obs-pgadminrestart:unless-stoppedports:-"5050:80"# pgAdmin UI portenv_file:-.env# Load environment variablesenvironment:PGADMIN_DEFAULT_EMAIL:${PGADMIN_DEFAULT_EMAIL:-admin@admin.com}PGADMIN_DEFAULT_PASSWORD:${PGADMIN_DEFAULT_PASSWORD:-admin}volumes:-pgadmin_data:/var/lib/pgadmin# pgAdmin configuration storagedepends_on:-obs-postgres# PostgreSQL must be readynetworks:-app_network# Can connect to PostgreSQL
# Connect to databasedockerexec-itobs-postgrespsql-Upostgres-dtodo_db
# List databasesdockerexec-itobs-postgrespsql-Upostgres-c"\l"# List tablesdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"\dt"# Check database sizedockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT pg_size_pretty(pg_database_size('todo_db'));"
Health Checks
# Check if PostgreSQL is readydockerexecobs-postgrespg_isready-Upostgres-dtodo_db
# Expected output:# /var/run/postgresql:5432 - accepting connections# Check via Docker healthdockerps--filter"name=obs-postgres"--format"{{.Status}}"
# Check active connectionsdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT * FROM pg_stat_activity;"# Check table sizesdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT relname AS table, pg_size_pretty(pg_total_relation_size(relid)) AS size FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;"# Check slow queries (if logging enabled)dockerlogsobs-postgres|grep"duration:"
Purpose: Persists database files across container restarts
Lifetime: Survives docker compose down (only removed with -v flag)
Backup Volume Data
# Backup volume to tardockerrun--rm-vdjango-observability_postgres_data:/data-v$(pwd):/backupalpinetarczf/backup/postgres_backup.tar.gz-C/data.
# Restore volume from tardockerrun--rm-vdjango-observability_postgres_data:/data-v$(pwd):/backupalpinetarxzf/backup/postgres_backup.tar.gz-C/data
Integration Points
Service
Connection
Purpose
Django
obs-postgres:5432
Application data storage
pgAdmin
obs-postgres:5432
Database management
Prometheus
Via django-prometheus
Query metrics
Monitoring with Prometheus
The django-prometheus backend provides automatic query metrics:
Metric
Description
django_db_query_duration_seconds
Query execution time
django_db_query_total
Total query count
django_db_query_errors_total
Failed query count
Sample PromQL Queries
# Database query raterate(django_db_query_total[5m])# Average query durationrate(django_db_query_duration_seconds_sum[5m])/rate(django_db_query_duration_seconds_count[5m])# Error raterate(django_db_query_errors_total[5m])
Troubleshooting
Connection Refused
# Check if PostgreSQL is runningdockerps|greppostgres
# Check PostgreSQL logsdockerlogsobs-postgres
# Verify network connectivitydockerexecobs-djangopingobs-postgres
# Check if port is accessibledockerexecobs-djangonc-zvobs-postgres5432
Authentication Failed
# Verify credentials in .envcatdjango_app/.env|grepDB_
# Check PostgreSQL user existsdockerexec-itobs-postgrespsql-Upostgres-c"\du"# Reset password if neededdockerexec-itobs-postgrespsql-Upostgres-c"ALTER USER postgres PASSWORD 'new_password';"
Database Not Found
# List databasesdockerexec-itobs-postgrespsql-Upostgres-c"\l"# Create database if missingdockerexec-itobs-postgrespsql-Upostgres-c"CREATE DATABASE todo_db;"# Check Django migrationsdockerexecobs-djangopythonmanage.pyshowmigrations
Performance Issues
# Check connection countdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT count(*) FROM pg_stat_activity;"# Check locksdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT * FROM pg_locks;"# Check table bloatdockerexec-itobs-postgrespsql-Upostgres-dtodo_db-c"SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) AS size FROM pg_tables WHERE schemaname = 'public' ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC;"