# PostgreSQL Stack Shared PostgreSQL-Datenbank für Application-Stacks (Staging und Production). ## Features - PostgreSQL 16 für Application-Datenbank - Automatische Backups (täglich um 2 Uhr) - Backup-Retention (7 Tage) - Health Checks - Optimierte Performance-Konfiguration ## Voraussetzungen - Infrastructure Network muss existieren - App-Internal Network wird von diesem Stack erstellt ## Setup ### 1. Infrastructure Network erstellen ```bash docker network create infrastructure ``` ### 2. Secrets erstellen ```bash # PostgreSQL Passwort openssl rand -base64 32 > secrets/postgres_password.txt chmod 600 secrets/postgres_password.txt ``` ### 3. Stack deployen ```bash docker compose up -d ``` ### 4. Datenbanken erstellen ```bash # Staging-Datenbank erstellen docker compose exec postgres psql -U postgres -c "CREATE DATABASE michaelschiemer_staging;" # Production-Datenbank existiert bereits (michaelschiemer) ``` ## Networks **infrastructure:** - Externes Network (muss vorher erstellt werden) - Für interne Infrastruktur-Kommunikation **app-internal:** - Wird von diesem Stack erstellt - Wird von Application-Stacks genutzt - Für Application ↔ PostgreSQL Kommunikation ## Volumes - `postgres-data` - PostgreSQL-Daten (persistent) - `postgres-backups` - Automatische Backups ## Datenbanken - `michaelschiemer` - Production-Datenbank - `michaelschiemer_staging` - Staging-Datenbank (muss manuell erstellt werden) ## Backups Backups werden automatisch täglich um 2 Uhr erstellt und in `/backups` gespeichert. **Manuelles Backup:** ```bash docker compose exec postgres-backup sh -c "PGPASSWORD=\$(cat /run/secrets/postgres_password) pg_dump -h postgres -U postgres -d michaelschiemer -F c -f /backups/manual_backup_$(date +%Y%m%d_%H%M%S).dump" ``` **Backup wiederherstellen:** ```bash docker compose exec -T postgres psql -U postgres -d michaelschiemer < backup_file.sql ``` ## Troubleshooting ### PostgreSQL startet nicht ```bash # Logs prüfen docker compose logs -f postgres # Volume-Berechtigungen prüfen docker compose exec postgres ls -la /var/lib/postgresql/data ``` ### Verbindungsprobleme von Application 1. Prüfe, ob Application im `app-internal` Network ist 2. Prüfe PostgreSQL-Logs 3. Prüfe Network-Verbindung: ```bash docker network inspect app-internal ``` ### Backup-Probleme ```bash # Backup-Logs prüfen docker compose logs -f postgres-backup # Backup-Verzeichnis prüfen docker compose exec postgres-backup ls -la /backups ```