| 1 | # Local PostgreSQL Backup And Restore |
| 2 | |
| 3 | DEV PostgreSQL runs in Kubernetes and stores data on the `postgres-data-dev` PVC. Do not back up or restore by copying container filesystem data while Postgres is running; use logical backups. |
| 4 | |
| 5 | Create a timestamped custom-format backup: |
| 6 | |
| 7 | ```powershell |
| 8 | $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" |
| 9 | New-Item -ItemType Directory -Force -Path ".tmp\backups" | Out-Null |
| 10 | kubectl -n ai-investment exec deploy/postgres -- pg_dump -U investment -d investment -Fc > ".tmp\backups\investment-$timestamp.dump" |
| 11 | ``` |
| 12 | |
| 13 | Capture globals when role-level metadata matters: |
| 14 | |
| 15 | ```powershell |
| 16 | $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" |
| 17 | kubectl -n ai-investment exec deploy/postgres -- pg_dumpall -U investment --globals-only > ".tmp\backups\investment-globals-$timestamp.sql" |
| 18 | ``` |
| 19 | |
| 20 | Restore into an initialized DEV database: |
| 21 | |
| 22 | ```powershell |
| 23 | Get-Content ".tmp\backups\investment-YYYYMMDD-HHMMSS.dump" -AsByteStream | kubectl -n ai-investment exec -i deploy/postgres -- pg_restore -U investment -d investment --clean --if-exists --no-owner --no-privileges |
| 24 | ``` |
| 25 | |
| 26 | Retrieve the database password from the Kubernetes Secret only for commands that require an explicit password. Do not commit passwords or generated dumps. |