.dump/.sql or physical tarballs). Test backups periodically by restoring them to a test environment.sudo systemctl stop postgresql
/var/log/postgresql/) to determine the deletion time, affected tables, and whether WAL (Write-Ahead Logging) files cover the lost data.Logical backups use SQL-formatted files, ideal for selective restoration (e.g., a single table).
pg_dump:-F c) for compressibility and flexibility. Include blobs (-b) and verbose output (-v):pg_dump -U postgres -F c -b -v -f "/path/to/backup.dump" database_name
pg_restore:.dump files, use pg_restore to recreate the database schema and data. Example for a specific database:sudo -u postgres pg_restore -U postgres -d target_database /path/to/backup.dump
For compressed backups, decompress first (gunzip backup.dump.gz) before restoring.Physical backups copy database files directly, faster for large datasets but less flexible.
pg_basebackup:-F t flag outputs a tarball, and -z compresses it:pg_basebackup -F t -z -P -D /path/to/physical_backup -h localhost -p 5432 -U postgres
sudo systemctl stop postgresql
sudo rm -rf /var/lib/postgresql/*/data/*
sudo cp -r /path/to/physical_backup/* /var/lib/postgresql/*/data/
sudo chown -R postgres:postgres /var/lib/postgresql/*/data
sudo systemctl start postgresql
This method restores the database to the state at the time of the backup.PITR uses WAL files to recover to a specific timestamp or transaction, critical for accidental deletions or corruptions.
postgresql.conf to turn on archiving and set the WAL level:wal_level = replica
archive_mode = on
archive_command = 'cp %p /path/to/wal_archive/%f'
max_wal_senders = 2
wal_keep_segments = 100
Restart PostgreSQL to apply changes:sudo systemctl restart postgresql
pg_basebackup) to the data directory.postgresql.auto.conf in the data directory with recovery settings:restore_command = 'cp /path/to/wal_archive/%f %p'
recovery_target_time = 'YYYY-MM-DD HH:MI:SS'
recovery_target_timeline = 'latest'
sudo pg_ctl start -D /var/lib/postgresql/*/data -o "-p 5433"
For corrupted WAL files or physical data damage, use specialized tools:
pg_resetwal: Resets WAL logs and pg_control to recover from corruption (use with caution—this can cause data loss if misapplied).pg_repack (for defragmentation/recovery) or Wondershare Recoverit (for physical file recovery) may help, but success is not guaranteed.shared_buffers) if needed.