
PostgreSQL, part 3: backup — pg_dump, pg_basebackup, pgBackRest, and point-in-time recovery
🌐 Machine-translated from Russian. Show original
In this part:
Three ways and when which#
| Method | What copies | Pros | Cons |
|---|---|---|---|
| pg_dump | one database logically (SQL / your own format) | simple, transferable between versions, you can restore one table | slowly on large databases, only a "snapshot at startup" |
| pg_basebackup | the entire cluster is physically | built-in, fast, replica base | the entire cluster as a whole, you do the rotation and verification yourself |
| pgBackRest | the entire cluster + WAL archive | incremental backups, compression, encryption, S3, parallelism, PITR | a separate tool, you need to configure |
A practical setup for production: pgBackRest (or another physical backup with a WAL archive) as the main method + night pg_dump important bases as "insurance" and for one-off tasks.
pg_dump and pg_restore#
sudo -u postgres mkdir -p /backup/dump
# одна база в собственном сжатом формате
sudo -u postgres pg_dump -Fc -Z zstd:5 -d appdb -f /backup/dump/appdb-$(date +%F).dump
# роли и права всего кластера (в pg_dump их нет!)
sudo -u postgres pg_dumpall --globals-only -f /backup/dump/globals-$(date +%F).sql
For large databases — catalog format and parallel export: pg_dump -Fd -j 4 -d appdb -f /backup/dump/appdb-dir.
Restore to the new database:
sudo -u postgres createdb -O app appdb_restore
sudo -u postgres pg_restore -j 4 -d appdb_restore /backup/dump/appdb-2026-11-10.dump
# одна таблица
sudo -u postgres pg_restore -d appdb_restore -t orders /backup/dump/appdb-2026-11-10.dump
⚠️ You can dump the 1C database with a standard pg_dump, but the main backup for 1C is physical (or unloading
.dtthe platform's transfer tools). Details — see the step about backups in the "1C Server on Windows and Linux" series.
pg_basebackup — physical copy#
sudo -u postgres pg_basebackup -D /backup/base/$(date +%F) -Ft -z -X stream -P -c fast
-Ft -z— tar archives with compression;-X stream— immediately retrieve the WAL needed for coordinated restoration;-c fast— do not wait for the scheduled checkpoint.
With PostgreSQL 17, pg_basebackup can perform incremental copies: enable summarize_wal = on, then pg_basebackup --incremental=/backup/base/full/backup_manifest -D /backup/base/incr1, and when restoring it, merge the chain with the utility pg_combinebackup. This is a working option, but rotation, validation, and storage will have to be written by you yourself — so let's take pgBackRest next.
How point-in-time recovery (PITR) works#
- Once a day (week) — full physical copy.
- Between copies, PostgreSQL continuously archives WAL — each populated log segment goes to storage.
- To recover, a copy is taken and WAL is "played" until the desired moment: for example, a minute before the incorrect
DELETE.
Without the WAL archive, you will lose all changes since the last copy.
pgBackRest: installation and configuration#
pgBackRest is a free backup tool, the current version as of November 2026 is 2.60. The package is available in the PGDG repository.
sudo apt install -y pgbackrest
sudo mkdir -p /var/lib/pgbackrest && sudo chown postgres:postgres /var/lib/pgbackrest && sudo chmod 750 /var/lib/pgbackrest
/etc/pgbackrest/pgbackrest.conf:
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
compress-type=zst
process-max=4
start-fast=y
log-level-console=info
[main]
pg1-path=/var/lib/postgresql/18/main
💡
/var/lib/pgbackreston the same disk as the database is just for a start. In production, the repository is on a separate backup server (pgBackRest can work via SSH) or in an S3-compatible storage (repo1-type=s3). You can keep two repositories at once:repo1locally,repo2outside the office.
Enable WAL archiving:
sudo tee /etc/postgresql/18/main/conf.d/40-archive.conf >/dev/null <<'EOF'
archive_mode = on
archive_command = 'pgbackrest --stanza=main archive-push %p'
EOF
sudo systemctl restart postgresql@18-main
Create a "stanza" (cluster description in the repository) and check that the archive is working:
sudo -u postgres pgbackrest --stanza=main stanza-create
sudo -u postgres pgbackrest --stanza=main check
pgBackRest: copies and schedule#
sudo -u postgres pgbackrest --stanza=main --type=full backup
sudo -u postgres pgbackrest --stanza=main --type=diff backup
sudo -u postgres pgbackrest --stanza=main info
| Type | What copies |
|---|---|
full | all files |
diff | changes since the last full |
incr | changes from the last copy of the |
Schedule — via cron by user postgres (sudo crontab -u postgres -e):
30 1 * * 0 pgbackrest --stanza=main --type=full backup
30 1 * * 1-6 pgbackrest --stanza=main --type=diff backup
With repo1-retention-full=2 pgBackRest itself deletes old copies and unnecessary WAL archives, leaving the last two full ones with their chains.
Point-in-time recovery#
Scenario: at 14:32 someone deleted data. Restoring the cluster at 14:30.
sudo systemctl stop postgresql@18-main
sudo -u postgres pgbackrest --stanza=main --delta \
--type=time "--target=2026-11-10 14:30:00+05" --target-action=promote restore
sudo systemctl start postgresql@18-main
--delta only overwrites different files — faster than restoring to an empty directory. pgBackRest will write restore_command itself and create recovery.signal. After launching, PostgreSQL will lose WAL until 14:30 and become available for write.
⚠️ Point-in-time recovery rolls back the entire cluster, including all databases. If one table is affected, restore the copy to another server (or to another directory with a different port) and move the necessary rows from there.
Recovery test — mandatory#
A backup from which you have never recovered is hope, not a backup. Once a month:
- Launch a test VM with the same PostgreSQL and pgBackRest with repository access.
- Restore the last copy:
pgbackrest --stanza=main restore. - Run PostgreSQL, check the key tables and row count, and run the application on a test copy.
- Record the recovery time — this is your real RTO.
Additionally, pgBackRest can perform verify — checking the integrity of files in the repository.
Backup checklist#
- The WAL archive works:
pgbackrest checkerror-free,pg_stat_archiverno growingfailed_count - Full copy at least once a week, difference copy daily
- Off-site and off-site copy of the database ( rule 3-2-1)
- Roles:
pg_dumpall --globals-onlyas part of night missions - Monthly recovery test with time recording
- Alert if backup failed (part 5)
Next is PostgreSQL, part 4: streaming replication, replica failover, and Patroni overview.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.