PostgreSQL backup and recovery
Use the backup and restore scripts from an administrative runner with the PostgreSQL client tools for the database major version. Set libpq connection values such as PGHOST, PGPORT, PGUSER, and PGSSLMODE=verify-full. Supply credentials through an access-restricted PGPASSFILE provisioned by the secret manager. Confirm the selected host, role, and source database before dumping. Verify that the dump role can read all application tables.
Take and retain a backup
Set PGDATABASE to the source application database and choose a new filename in a private backup directory.
sh deploy/production/backup.sh /secure/backups/potik-2026-11-22.dumpThe command uses a PostgreSQL custom-format snapshot and checks its table of contents before atomically publishing it at the requested path. Move the archive to encrypted off-host storage and apply your retention policy. Record its timestamp, PostgreSQL version, application migration version, and checksum. Store database role provisioning, runtime secrets, release digests, and Redis recovery data separately. Logical archives contain application data and encrypted browser session credentials. Limit access to the same operators authorized to handle the database.
Plan separate PostgreSQL point-in-time recovery if your recovery point requires it. PostgreSQL snapshots do not capture queued jobs already relayed to Redis. Preserve the Redis AOF for queue recovery. For a coordinated maintenance backup, pause writes and drain the worker and outbox before capturing both stores. During recovery, inspect the outbox and pending and archived Asynq tasks before deciding whether to replay jobs. Inbox records persist in PostgreSQL.
Restore and validate
Point libpq at an isolated PostgreSQL target. The restore role needs permission to create a database and the server must support the schema's extensions and encodings.
sh deploy/production/restore.sh /secure/backups/potik-2026-11-22.dump potik_restoredThe script creates a new database and applies the dump in one transaction. Source ownership and grants are omitted. Recreate required roles and grants for the target environment. If import fails, the transaction rolls back and the new database remains available for diagnosis. Use a different new database name for a retry until an operator removes the failed target.
Before switching an environment to the restored database:
- Verify the expected migration version and compare known records, including recent changes at the backup point.
- Start the matching application release on the restored data in isolation. Verify readiness, authenticated access boundaries, and an authorized staging read and write. Use staging identity and mail credentials, avoiding real notifications. Configure a fresh browser session encryption key if restored sessions should be invalidated.
- Confirm the expected data-loss window against the recorded backup time and measure time to restore service. Keep the previous database available and fenced from writes until the cutover and reconciliation are complete. Treat a live database endpoint change as a production operation.
The deployment rehearsal creates and restores its own synthetic database. Run a periodic restore using the actual encrypted backup set within a controlled recovery environment to validate retention, credentials, and elapsed recovery time.