In this article
- What you will learn
- Prerequisites
- How it works
- Install pgBackRest and prepare the directories
- Configure the repository and the stanza
- Take full, differential and incremental backups
- Retention: how old backups and WAL go away
- Restore drill: recover to a point in time in a test cluster
- Rebuild a damaged cluster with --delta
- Monitor backups and archiving
- Production layout: a dedicated repository host
- Common mistakes and how to fix them
- Practice exercise
- Quick reference
- Summary and what's next
This is Part 21 of the PostgreSQL DBA series. In Part 20 we recovered a database to the moment before a mistake using a base backup and archived WAL, all by hand. In this part you will set up pgBackRest, the backup tool many teams run in production, so the same jobs become a few short commands: full, differential and incremental backups, automatic WAL archiving, encryption, retention, and restores to any point in time.
By the end you will have pgBackRest installed on Ubuntu with PostgreSQL 18, a working, encrypted backup repository, a backup schedule, and you will have done two restores yourself: a point-in-time restore into a separate test cluster, and a full restore of the main cluster after losing a critical file.
What you will learn
- What pgBackRest adds on top of
pg_basebackupand a hand-writtenarchive_command - How to install pgBackRest from the PGDG repository and write
/etc/pgbackrest.conf - What a stanza is, and how to create and check one
- How full, differential and incremental backups differ, and how retention removes old ones
- How to restore to a point in time into a test cluster, and how to rebuild a damaged cluster with
--delta - How to monitor backups and archiving from the shell and from SQL
Prerequisites
- Parts 1 to 20 of this series, especially Part 15 (WAL), Part 18 (pg_basebackup), Part 19 (WAL archiving) and Part 20 (point-in-time recovery).
- One Ubuntu server (we call it db1) with PostgreSQL 18 from the PGDG apt repository, cluster 18 main on port 5432, installed as in Part 2.
- A user with sudo rights. Every PostgreSQL and pgBackRest command runs as the
postgresoperating system user. - Free disk space for the backup repository. For practice, a few hundred MB is enough.
- If you followed Part 19, remove or replace the
archive_commandyou set there. A cluster should have one archiver, and in this part pgBackRest takes over that job.
How it works
In Parts 18 to 20 you built a backup system by hand: pg_basebackup for the data files and a cp command in archive_command for the WAL. Nothing checked that the WAL arrived, nothing deleted old backups, and nothing was encrypted.
pgBackRest is an open source tool that does all of these jobs. If pg_basebackup is a camera that takes one photo of your data, pgBackRest is a photo library: it takes the photos, files them by date, keeps the right WAL next to each one, removes the oldest by a rule you set, and can rebuild the database from any of them.
A few words you will see everywhere in pgBackRest:
| Term | Meaning |
|---|---|
| Repository | Where pgBackRest stores backups and archived WAL. Here /var/lib/pgbackrest; in production often a backup host or S3. |
| Stanza | The configuration for one PostgreSQL cluster. The user guide recommends naming it after what the cluster does (app), not main, because primary and standbys share it. |
| Full backup | Every file in the cluster. Depends on no other backup. |
| Differential backup (diff) | Only the files changed since the last full backup. |
| Incremental backup (incr) | Only the files changed since the last backup of any type. This is the default type. |
| archive-push / archive-get | Commands PostgreSQL calls to store a WAL segment (archive_command) and fetch one during recovery (restore_command). |
| Expire | Removes backups and WAL beyond your retention. Runs after every successful backup. |
We build the simplest layout, the one the user guide starts with: everything on one server.
db1 (Ubuntu, PostgreSQL 18, cluster "18 main", port 5432)
PostgreSQL --archive_command--> pgbackrest archive-push --+
v
pgbackrest backup ------- data files --------> repository /var/lib/pgbackrest
(encrypted, zstd compressed)
pgbackrest restore <------ backup + WAL -------+Install pgBackRest and prepare the directories
Step 1: Install the package
On db1, as a user with sudo. pgBackRest is in the same PGDG apt repository you added in Part 2, so one command installs it.
sudo apt-get install -y pgbackrestCheck that the program runs as the postgres user, and look at what the package created.
sudo -u postgres pgbackrest version
ls -ld /etc/pgbackrest.conf /var/lib/pgbackrest /var/log/pgbackrest /var/spool/pgbackrestpgBackRest 2.60.1
-rw-r----- 1 postgres postgres 165 Nov 18 12:48 /etc/pgbackrest.conf
drwxr-x--- 2 postgres postgres 4096 Nov 18 12:48 /var/lib/pgbackrest
drwxr-x--- 2 postgres postgres 4096 Nov 18 12:48 /var/log/pgbackrest
drwxr-x--- 2 postgres postgres 4096 Nov 18 12:48 /var/spool/pgbackrestThe version number depends on when you install. The Ubuntu package already created the repository directory /var/lib/pgbackrest, the log directory and a spool directory, all owned by postgres with mode 750, plus a sample configuration file /etc/pgbackrest.conf that only postgres can read. pgBackRest first looks for /etc/pgbackrest/pgbackrest.conf and falls back to /etc/pgbackrest.conf if that does not exist, so we use the file the package created.
Configure the repository and the stanza
Step 2: Create an encryption passphrase
On db1, as any user. pgBackRest can encrypt everything it writes to the repository, on the database server side, before the data leaves it. The user guide recommends a long random passphrase and suggests this command to make one:
openssl rand -base64 48iztoVhoipgQMwWUlvSo8phR2li6ipfWt/NK9QG/W/bgqjC8RXh0AE2spibHN9GTDNever reuse a passphrase printed in an article, including this one.
Step 3: Write /etc/pgbackrest.conf
On db1, as a user with sudo. Open the file with sudo -u postgres nano /etc/pgbackrest.conf so the owner stays postgres. The package's sample file looks like this:
[global]
repo1-path=/var/lib/pgbackrest
#repo1-retention-full=2
#repo1-cipher-pass=...
#repo1-cipher-type=aes-256-cbc
#[main]
#pg1-path=...Replace it with the following, using your own passphrase from Step 2:
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
repo1-cipher-type=aes-256-cbc
repo1-cipher-pass=<your passphrase from Step 2>
compress-type=zst
repo1-bundle=y
repo1-block=y
process-max=2
start-fast=y
log-level-file=detail
[app]
pg1-path=/var/lib/postgresql/18/main[global] holds settings for every stanza and [app] is our stanza. Comments go on their own line and values are not quoted. From the configuration reference:
| Setting | What it does |
|---|---|
| repo1-path | Where backups and archived WAL are stored. Default /var/lib/pgbackrest. |
| repo1-retention-full | Number of full backups to keep. Their diff and incr backups expire with them. |
| repo1-cipher-type | Repository encryption: none (default) or aes-256-cbc. |
| repo1-cipher-pass | The encryption passphrase. |
| compress-type | Compression. Default gz; the user guide recommends zst for new repositories (much faster, similar size). |
| repo1-bundle | Combines small files into bundles to save space and time. Default n. |
| repo1-block | Block incremental: diff and incr backups store only changed parts of files. Needs repo1-bundle=y. |
| process-max | Processes for compression and transfer. Default 1. Keep it low enough not to slow the database. |
| start-fast | Forces a checkpoint so the backup starts at once. Default n. |
| log-level-file | Detail of the log files in /var/log/pgbackrest. Default info. |
| pg1-path | The stanza's data directory. Must equal the data_directory PostgreSQL reports. |
We leave log-level-console at its default, warn, so the PostgreSQL log stays quiet when PostgreSQL calls pgBackRest for each WAL segment. By hand, add --log-level-console=info to see progress.
Verify: make sure pg1-path matches the server, and ask pgBackRest about the stanza.
sudo -u postgres psql -Atc "SHOW data_directory;"
sudo -u postgres pgbackrest --stanza=app info/var/lib/postgresql/18/main
stanza: app
status: error (missing stanza path)This error is expected: the stanza does not exist in the repository until Step 5.
Step 4: Send WAL to pgBackRest
On db1, as a user with sudo. A backup of a running cluster is only usable with the WAL written while it ran, so pgBackRest requires WAL archiving. You know these two settings from Part 19. Edit /etc/postgresql/18/main/postgresql.conf (see Part 5 for how this file works):
#archive_mode = off # enables archiving; off, on, or always
#archive_command = '' # command to use to archive a WAL filearchive_mode = on
archive_command = 'pgbackrest --stanza=app archive-push %p'PostgreSQL replaces %p with the path of the finished WAL segment. archive-push compresses it, encrypts it and stores it in the repository, and returns exit status zero only when the file is safely stored, which is what PostgreSQL requires of an archive command. The documented defaults are archive_mode = off and an empty archive_command. wal_level must stay at its default replica (or logical); archiving cannot be enabled with minimal.
The PostgreSQL documentation says archive_mode can only be set at server start, so this change needs a restart, not a reload. (archive_command alone can be changed later with a reload.)
sudo systemctl restart postgresql@18-mainVerify:
sudo -u postgres psql -c "SELECT name, setting FROM pg_settings WHERE name IN ('archive_mode','archive_command','wal_level') ORDER BY name;" name | setting
-----------------+-----------------------------------------
archive_command | pgbackrest --stanza=app archive-push %p
archive_mode | on
wal_level | replica
(3 rows)Undo: set archive_mode = off and restart again. Do not leave archive_mode = on with a broken command for long: PostgreSQL keeps every WAL segment it could not archive in pg_wal, and the disk slowly fills up.
Step 5: Create the stanza
On db1, as postgres. stanza-create sets up the stanza's folders and info files in the repository.
sudo -u postgres pgbackrest --stanza=app --log-level-console=info stanza-create2026-11-18 12:49:23.775 P00 INFO: stanza-create command begin 2.60.1: --exec-id=1252-22436de4 --log-level-console=info --log-level-file=detail --pg1-path=/var/lib/postgresql/18/main --repo1-cipher-pass=<redacted> --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --stanza=app
2026-11-18 12:49:23.781 P00 INFO: stanza-create for stanza 'app' on repo1
2026-11-18 12:49:23.784 P00 INFO: stanza-create command end: completed successfully (11ms)| Option | What it does |
|---|---|
| --stanza=app | The stanza (cluster) the command works on. |
| --log-level-console=info | Prints info, warnings and errors on the screen. The default, warn, prints only warnings and errors. |
The first line shows every option in effect, with the passphrase <redacted>.
Step 6: Check the configuration
On db1, as postgres. The user guide recommends running check after stanza-create. It forces PostgreSQL to switch to a new WAL segment and waits until that segment arrives in the repository, so it tests the whole archive path end to end.
sudo -u postgres pgbackrest --stanza=app --log-level-console=info check2026-11-18 12:49:23.796 P00 INFO: check command begin 2.60.1: --exec-id=1255-3a0cd5c6 --log-level-console=info ...
2026-11-18 12:49:23.802 P00 INFO: check repo1 configuration (primary)
2026-11-18 12:49:23.816 P00 INFO: check repo1 archive for WAL (primary)
2026-11-18 12:49:24.117 P00 INFO: WAL segment 000000010000000000000001 successfully archived to '/var/lib/pgbackrest/archive/app/18-1/0000000100000000/000000010000000000000001-10adebfde2f56a5adb602ba41819d3c0325ecffe.zst' on repo1
2026-11-18 12:49:24.117 P00 INFO: check command end: completed successfully (323ms)The archived file name has three parts: the WAL segment name, a SHA-1 checksum pgBackRest uses to detect damage, and .zst for zstd compression. 18-1 means PostgreSQL 18, first database system in this stanza. Your segment number will differ on a server that has been running for a while.
Verify from PostgreSQL's side with the pg_stat_archiver view:
sudo -u postgres psql -x -c "SELECT archived_count, last_archived_wal, last_archived_time, failed_count FROM pg_stat_archiver;"-[ RECORD 1 ]------+------------------------------
archived_count | 1
last_archived_wal | 000000010000000000000001
last_archived_time | 2026-11-18 12:49:23.934199+00
failed_count | 0Take full, differential and incremental backups
For the examples we use a small orders table in a database called shop. Create it on db1 as postgres:
CREATE DATABASE shop;
\c shop
CREATE TABLE orders (
order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL,
amount numeric(10,2) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO orders (customer, amount)
SELECT 'customer_' || g, (g * 10.5)::numeric(10,2)
FROM generate_series(1, 1000) AS g;Step 7: Take the first full backup
On db1, as postgres. The cluster stays online during the backup; users keep working.
sudo -u postgres pgbackrest --stanza=app --type=full --log-level-console=info backup2026-11-18 12:49:26.474 P00 INFO: backup command begin 2.60.1: --compress-type=zst ... --stanza=app --start-fast --type=full
2026-11-18 12:49:26.483 P00 INFO: execute backup start: backup begins after the requested immediate checkpoint completes
2026-11-18 12:49:26.524 P00 INFO: backup start archive = 000000010000000000000002, lsn = 0/2000028
2026-11-18 12:49:26.524 P00 INFO: check archive for segment 000000010000000000000002
2026-11-18 12:49:27.362 P00 INFO: execute backup stop and wait for all WAL segments to archive
2026-11-18 12:49:27.384 P00 INFO: backup stop archive = 000000010000000000000003, lsn = 0/3000050
2026-11-18 12:49:27.386 P00 INFO: check archive for segment(s) 000000010000000000000002:000000010000000000000003
2026-11-18 12:49:27.493 P00 INFO: new backup label = 20261118-124926F
2026-11-18 12:49:27.514 P00 INFO: full backup size = 29.6MB, file total = 1267
2026-11-18 12:49:27.514 P00 INFO: backup command end: completed successfully (1042ms)
2026-11-18 12:49:27.514 P00 INFO: expire command begin 2.60.1: ...
2026-11-18 12:49:27.514 P00 INFO: expire command end: completed successfully (0ms)| Option | What it does |
|---|---|
| --type=full | Backup type: full copies all files, diff copies changes since the last full, incr copies changes since the last backup. The default is incr; with no full backup yet, pgBackRest warns and takes a full one instead. |
| backup | The command: back up the cluster named by the stanza. |
The log shows the steps from Part 18 done for you: a checkpoint, the backup start, the file copy, the stop, and a wait until WAL ...02 to ...03 is archived. Only then is the backup complete. The label 20261118-124926F is the start time plus F for full. expire then runs automatically; nothing is old enough to remove yet.
Step 8: Look at the backup with info
sudo -u postgres pgbackrest infostanza: app
status: ok
cipher: aes-256-cbc
db (current)
wal archive min/max (18): 000000010000000000000001/000000010000000000000003
full backup: 20261118-124926F
timestamp start/stop: 2026-11-18 12:49:26+00 / 2026-11-18 12:49:27+00
wal start/stop: 000000010000000000000002 / 000000010000000000000003
database size: 29.6MB, database backup size: 29.6MB
repo1: backup size: 3.6MBdatabase size is the uncompressed size of the cluster; repo1: backup size is what the backup takes in the repository after compression. wal start/stop is the WAL range you need to make this backup consistent when you restore it, and timestamp stop tells you the earliest time you can recover to with this backup.
Step 9: Take differential and incremental backups
On db1, as postgres. Change some data between backups so there is something to copy:
sudo -u postgres psql -d shop -c "INSERT INTO orders (customer, amount) SELECT 'customer_' || g, 99.00 FROM generate_series(1001, 1500) AS g;"
sudo -u postgres pgbackrest --stanza=app --type=diff --log-level-console=info backup
sudo -u postgres psql -d shop -c "UPDATE orders SET amount = amount + 1 WHERE order_id <= 100;"
sudo -u postgres pgbackrest --stanza=app --type=incr --log-level-console=info backupP00 INFO: last backup label = 20261118-124926F, version = 2.60.1
P00 INFO: new backup label = 20261118-124926F_20261118-124930D
P00 INFO: diff backup size = 320.3KB, file total = 1267
P00 INFO: backup command end: completed successfully (1426ms)
P00 INFO: last backup label = 20261118-124926F_20261118-124930D, version = 2.60.1
P00 INFO: new backup label = 20261118-124926F_20261118-124932I
P00 INFO: incr backup size = 208.3KB, file total = 1267
P00 INFO: backup command end: completed successfully (960ms)The diff backup's label starts with the full backup it depends on and ends in D; the incremental ends in I. Each copied only a few hundred KB instead of 29.6 MB. pgbackrest info now lists all three, and the incremental shows backup reference total: 1 full, 1 diff: to restore it, pgBackRest needs both of those backups too.
| Type | Copies | Restore needs | Typical use |
|---|---|---|---|
| full | Everything | Only itself | Weekly, or daily for small databases |
| diff | Changes since the last full | The full + this diff | Daily between full backups |
| incr | Changes since the last backup of any type | The full + any diff + every incr in between | Several times a day on busy systems |
Step 10: Schedule the backups with cron
On db1, as postgres. pgBackRest has no scheduler of its own; the user guide uses cron. Open the postgres user's crontab with sudo -u postgres crontab -e and add a weekly full and a daily differential, the same pattern the user guide shows:
#m h dom mon dow command
30 01 * * 0 pgbackrest --type=full --stanza=app backup
30 01 * * 1-6 pgbackrest --type=diff --stanza=app backupThis runs a full backup at 01:30 every Sunday and a differential at 01:30 Monday to Saturday, in the server's time zone. Pick a quiet hour for your workload.
Verify: sudo -u postgres crontab -l prints the two lines back, and the next morning sudo -u postgres pgbackrest info lists the new backup.
Retention: how old backups and WAL go away
Step 11: Watch expire remove the oldest full backup
We set repo1-retention-full=2. The reference explains that a new backup must complete before the old one is counted out, so with a count of 2 there will be three full backups for a moment, and then the oldest is removed together with every diff and incr that depends on it. Take two more full backups to see it:
sudo -u postgres pgbackrest --stanza=app --type=full --log-level-console=info backup
sudo -u postgres pgbackrest --stanza=app --type=full --log-level-console=info backupP00 INFO: new backup label = 20261118-125016F
P00 INFO: repo1: 18-1 remove archive, start = 000000010000000000000001, stop = 000000010000000000000001
P00 INFO: new backup label = 20261118-125017F
P00 INFO: repo1: expire full backup set 20261118-124926F, 20261118-124926F_20261118-124930D, 20261118-124926F_20261118-124932I
P00 INFO: repo1: remove expired backup 20261118-124926F_20261118-124932I
P00 INFO: repo1: remove expired backup 20261118-124926F_20261118-124930D
P00 INFO: repo1: remove expired backup 20261118-124926F
P00 INFO: repo1: 18-1 remove archive, start = 000000010000000000000002, stop = 000000010000000000000009The first run removed only a WAL segment older than every backup, which can never be used. The second expired the first full backup with its diff and incr, and the WAL only they needed. Your recovery window is now from the oldest kept backup to the present; set repo1-retention-full (or repo1-retention-full-type=time with a number of days) to match how far back you must be able to go. Expire only runs after a successful backup, so failing backups also mean a growing repository.
Restore drill: recover to a point in time in a test cluster
Now the reason backups exist. Someone runs DELETE FROM orders; without a WHERE. As in Part 20, we restore a copy of the cluster to the moment before the mistake, check it, and copy the rows back. This time pgBackRest writes all the recovery settings for us.
db1
cluster "18 main" port 5432 (production, rows deleted)
cluster "18 restore" port 5433 (test copy, restored from repository /var/lib/pgbackrest)Step 12: Note the time, then make the mistake
On db1, as postgres. In real life you find the time from the application log or with pg_waldump as in Part 20. Here we write it down first. Use the time as PostgreSQL reports it, with the time zone offset: the user guide stresses this to avoid unexpected time zone conversions.
sudo -u postgres psql -Atc "SELECT now();"
sudo -u postgres psql -d shop -c "DELETE FROM orders;"
sudo -u postgres psql -Atc "SELECT pg_switch_wal();"2026-11-18 12:49:37.647005+00
DELETE 1500
0/8050CF0pg_switch_wal() closes the current WAL segment so it is archived now. On a busy server that happens on its own within seconds.
Step 13: Create an empty test cluster
On db1, as a user with sudo. Create a second cluster for the restore and empty its data directory.
sudo pg_createcluster 18 restore --port 5433
sudo -u postgres find /var/lib/postgresql/18/restore -mindepth 1 -delete| Option | What it does |
|---|---|
| 18 restore | Version and name of the new cluster. Its configuration goes in /etc/postgresql/18/restore/ and its data in /var/lib/postgresql/18/restore. |
| --port 5433 | Port for the new cluster, so it does not clash with main on 5432. |
| -mindepth 1 -delete | Deletes the contents of the directory but keeps the directory itself. |
Verify: pg_lsclusters shows 18 restore 5433 down, and sudo -u postgres ls /var/lib/postgresql/18/restore prints nothing. pgBackRest refuses to restore into a directory that still contains files (see Common mistakes).
Step 14: Restore to the time before the mistake
On db1, as postgres.
sudo -u postgres pgbackrest --stanza=app --log-level-console=info \
--pg1-path=/var/lib/postgresql/18/restore \
--type=time "--target=2026-11-18 12:49:37.647005+00" \
--target-action=promote --archive-mode=off restoreP00 INFO: repo1: restore backup set 20261118-124926F_20261118-124932I, recovery will start at 2026-11-18 12:49:32
P00 INFO: remap data directory to '/var/lib/postgresql/18/restore'
P00 INFO: write updated /var/lib/postgresql/18/restore/postgresql.auto.conf
P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
P00 INFO: restore size = 29.6MB, file total = 1267
P00 INFO: restore command end: completed successfully (594ms)| Option | What it does |
|---|---|
| --pg1-path=... | Restore into this data directory instead of the one in the configuration file (remap data directory in the log). |
| --type=time | Recover to the time given in --target. Other types: default (to the end of the archived WAL), immediate, lsn, xid, name, standby, preserve, none. |
| --target=... | The recovery target, here a timestamp with time zone. Quote the whole option because it contains a space. |
| --target-action=promote | What to do when the target is reached: pause (the default), promote (end recovery and open for writes on a new timeline) or shutdown. |
| --archive-mode=off | Writes archive_mode = off for the restored cluster, so this test copy never pushes its WAL into the production repository. Default preserve. |
Notice that we did not say which backup to use. With --type=time, pgBackRest picks a backup that finished before the target time, here the incremental. Look at what it wrote for PostgreSQL:
sudo -u postgres tail -5 /var/lib/postgresql/18/restore/postgresql.auto.conf# Recovery settings generated by pgBackRest restore on 2026-11-18 12:49:50
archive_mode = 'off'
restore_command = 'pgbackrest --pg1-path=/var/lib/postgresql/18/restore --stanza=app archive-get %f "%p"'
recovery_target_time = '2026-11-18 12:49:37.647005+00'
recovery_target_action = 'promote'These are the settings you wrote by hand in Part 20, and pgBackRest also created recovery.signal. pgBackRest does not start PostgreSQL, so you can still check this file first.
Step 15: Start the test cluster and check the data
On db1: start the cluster with sudo, then query it as postgres on port 5433.
sudo systemctl start postgresql@18-restore
sudo -u postgres psql -p 5433 -d shop -c "SELECT count(*), max(order_id) FROM orders;" -c "SELECT pg_is_in_recovery();" count | max
-------+------
1500 | 1500
(1 row)
pg_is_in_recovery
-------------------
f
(1 row)All 1,500 rows are back, and the cluster has left recovery. The server log /var/log/postgresql/postgresql-18-restore.log tells the story:
LOG: starting point-in-time recovery to 2026-11-18 12:49:37.647005+00
LOG: restored log file "000000010000000000000007" from archive
LOG: redo starts at 0/7000028
LOG: restored log file "000000010000000000000008" from archive
LOG: consistent recovery state reached at 0/7000100
LOG: recovery stopping before commit of transaction 746, time 2026-11-18 12:49:39.679831+00
LOG: selected new timeline ID: 2
LOG: archive recovery complete
LOG: database system is ready to accept connectionsTransaction 746 is our DELETE, committed two seconds after the target, so recovery stopped just before it. restored log file ... from archive is archive-get at work.
Step 16: Copy the rows back and remove the test cluster
On db1, as postgres. Dump the table's data from the test copy and load it into production. A data-only dump of a table with an identity column also sets its sequence, which is the setval line in the output.
sudo -u postgres bash -c "pg_dump -p 5433 -d shop -t orders --data-only | psql -q -p 5432 -d shop"
sudo -u postgres psql -d shop -Atc "SELECT count(*) FROM orders;" set_config
------------
(1 row)
setval
--------
1500
(1 row)
1500Production has its 1,500 rows again. Clean up the test cluster:
sudo pg_ctlcluster 18 restore stop
sudo pg_dropcluster 18 restore
pg_lsclustersVer Cluster Port Status Owner Data directory Log file
18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.logRebuild a damaged cluster with --delta
The second common case is not a user mistake but a broken server: a lost file, a bad disk, a failed upgrade. Here you want all committed data, so you restore with the default type, which replays every archived WAL segment. The --delta option lets pgBackRest keep the files that are still correct and replace only the ones that differ from the backup, which can save a lot of time on a large cluster.
Step 17: Simulate the damage
On db1, as a user with sudo. Stop the cluster and delete global/pg_control, the file PostgreSQL reads first at start-up. This is the same test the pgBackRest user guide uses.
sudo systemctl stop postgresql@18-main
sudo -u postgres rm /var/lib/postgresql/18/main/global/pg_control
sudo pg_ctlcluster 18 main startError: /usr/lib/postgresql/18/bin/pg_ctl /usr/lib/postgresql/18/bin/pg_ctl start -D /var/lib/postgresql/18/main -l /var/log/postgresql/postgresql-18-main.log -s -o -c config_file="/etc/postgresql/18/main/postgresql.conf" exited with status 1:
postgres: could not find the database system
Expected to find it in the directory "/var/lib/postgresql/18/main",
but could not open file "/var/lib/postgresql/18/main/global/pg_control": No such file or directory
pg_ctl: could not start server
Examine the log output.Step 18: Restore with --delta and start
On db1, as postgres, with the cluster stopped.
sudo -u postgres pgbackrest --stanza=app --delta --log-level-console=info restoreP00 INFO: repo1: restore backup set 20261118-125017F, recovery will start at 2026-11-18 12:50:17
P00 INFO: remove invalid files/links/paths from '/var/lib/postgresql/18/main'
P00 INFO: write updated /var/lib/postgresql/18/main/postgresql.auto.conf
P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
P00 INFO: restore size = 29.7MB, file total = 1268
P00 INFO: restore command end: completed successfully (292ms)| Option | What it does |
|---|---|
| --delta | Restore using checksums: files that already match the backup are kept, others are replaced, and files that are not in the backup are removed. Without it, the data directory must be empty. |
| (no --type) | Type default: recover to the end of the archived WAL stream. |
| (no --set) | Use the latest backup. Give --set=<label> to choose another. |
pgBackRest restores pg_control last, so a restore that is interrupted halfway leaves a cluster that will not start, instead of one that starts with half its files. Now start the cluster and check:
sudo systemctl start postgresql@18-main
sudo -u postgres psql -d shop -Atc "SELECT count(*) FROM orders;"
sudo -u postgres psql -Atc "SELECT pg_is_in_recovery(), timeline_id FROM pg_control_checkpoint();"1500
f|2All rows are there, including the ones we loaded back in Step 16, because recovery replayed every archived WAL segment. The cluster is out of recovery on timeline 2. A new timeline marks a new history after a restore, as you saw in Part 20.
Last step of every restore: run check and take a new full backup, so your newest backup belongs to the new timeline.
sudo -u postgres pgbackrest --stanza=app --log-level-console=info check
sudo -u postgres pgbackrest --stanza=app --type=full --log-level-console=info backupP00 INFO: WAL segment 00000002000000000000000E successfully archived to '/var/lib/pgbackrest/archive/app/18-1/0000000200000000/00000002000000000000000E-2bb98e5a7b648ac934e514166c4b6831e06af2c2.zst' on repo1
P00 INFO: check command end: completed successfully (128ms)
P00 INFO: new backup label = 20261118-125046F
P00 INFO: full backup size = 29.7MB, file total = 1268The archived segment name now starts with 00000002: WAL from timeline 2.
Monitor backups and archiving
Check two things every day: when the last backup finished, and whether WAL archiving is failing.
Step 19: Read backup status as JSON
On db1, as postgres. info --output=json is the machine-readable format the user guide keeps stable, unlike the text format. With jq (sudo apt-get install -y jq) a monitoring script can pull out single values:
sudo -u postgres pgbackrest --output=json --stanza=app info | jq -r '.[0].status.message'
sudo -u postgres pgbackrest --output=json --stanza=app info | jq -r '.[0].backup[-1].timestamp.stop | todate'
sudo -u postgres pgbackrest --output=json --stanza=app info | jq -r '.[0].archive[-1].max'ok
2026-11-18T12:50:47Z
000000020000000000000010The lines are the stanza status, the time the newest backup finished, and the newest archived WAL segment. Alert if the status is not ok or the last backup is older than your schedule allows. From PostgreSQL's side, a growing failed_count in pg_stat_archiver (Step 6) means WAL is piling up in pg_wal. pgBackRest's detailed logs are in /var/log/pgbackrest/, one file per stanza and command, such as app-backup.log.
Production layout: a dedicated repository host
For production, the pgBackRest user guide recommends a dedicated repository host: a separate server that stores the backups and WAL archive, so losing the database server does not take the backups with it. The backup command runs on the repository host and talks to db1 over SSH (or pgBackRest's own TLS server).
db1 (10.0.0.11, PostgreSQL 18) backup1 (10.0.0.21, pgBackRest only)
archive_command: archive-push ---- WAL ----> repo1-path=/var/lib/pgbackrest
postgres user <--- SSH ---> pgbackrest user, runs "backup" from cronIn short: the repository host's stanza gets pg1-host=db1 next to pg1-path, and db1's file gets repo1-host=backup1 instead of repo1-path. The pgBackRest version must be exactly the same on both hosts, or archiving and backups stop, and the repository is owned by a separate pgbackrest user. A second repository in object storage such as S3 adds an off-site copy. The SSH key setup takes several steps of its own, so follow the user guide's "Dedicated Repository Host" section and finish with check as in Step 6.
Common mistakes and how to fix them
ERROR: [031]: the 'info' command must not be run as the root user
P00 ERROR: [031]: the 'info' command must not be run as the root user
HINT: running as root can create files that the regular user cannot access, causing later commands to fail.You typed sudo pgbackrest .... Run every command as the owner of the cluster: sudo -u postgres pgbackrest .... Do not reach for --allow-root to make the error go away.
ERROR: [087]: archive_mode must be enabled
check or backup ran before Step 4, or after you set archive_mode = on but only reloaded. archive_mode needs a restart: sudo systemctl restart postgresql@18-main.
ERROR: [082]: WAL segment ... was not archived before the 60000ms timeout
P00 ERROR: [082]: WAL segment 000000020000000000000011 was not archived before the 10000ms timeout
HINT: check the archive_command to ensure that all options are correct (especially --stanza).
HINT: check the PostgreSQL server log for errors.WAL is not reaching the repository. Our test had a typo, --stanza=ap, in archive_command. The PostgreSQL log showed the real cause:
P00 ERROR: [037]: option 'pg1-path' must be specified when relative wal paths are used
LOG: archive command failed with exit code 37
DETAIL: The failed archive command was: pgbackrest --stanza=ap archive-push pg_wal/000000020000000000000011Fix the stanza name in postgresql.conf, run SELECT pg_reload_conf(); (a reload is enough for archive_command), and run check again. PostgreSQL retries the waiting segments by itself; pg_stat_archiver.last_archived_wal moves forward again.
ERROR: [040]: unable to restore to path ... because it contains files
P00 ERROR: [040]: unable to restore to path '/var/lib/postgresql/18/restore' because it contains files
HINT: try using --delta if this is what you intended.A plain restore needs an empty data directory. Empty it as in Step 13, or use --delta when you really mean to overwrite that cluster.
ERROR: [038]: unable to restore while PostgreSQL is running
P00 ERROR: [038]: unable to restore while PostgreSQL is running
HINT: presence of 'postmaster.pid' in '/var/lib/postgresql/18/main' indicates PostgreSQL is running.
HINT: remove 'postmaster.pid' only if PostgreSQL is not running.This check protects you. Stop the cluster with sudo systemctl stop postgresql@18-main and try again. Only delete postmaster.pid if you are sure no postgres process is running, for example after a crash.
ERROR: [027]: no database found
pgBackRest could not match pg1-path to the running cluster. Compare it with SHOW data_directory; character by character. After a major upgrade the path changes (for example /17/main to /18/main).
Practice exercise
- Insert 10 rows into
orders, note the time, thenUPDATE orders SET amount = 0;. Restore a copy to port 5433 at the noted time and confirm the amounts are correct there. - Take an incremental backup, then use
pgbackrest infoto find which other backups it depends on.
Quick reference
# Install and check
sudo apt-get install -y pgbackrest
sudo -u postgres pgbackrest version
# Make an encryption passphrase, then edit /etc/pgbackrest.conf ([global] + [app] stanza)
openssl rand -base64 48
sudo -u postgres nano /etc/pgbackrest.conf
# postgresql.conf: archive_mode = on
# archive_command = 'pgbackrest --stanza=app archive-push %p'
sudo systemctl restart postgresql@18-main
# Create and check the stanza
sudo -u postgres pgbackrest --stanza=app --log-level-console=info stanza-create
sudo -u postgres pgbackrest --stanza=app --log-level-console=info check
# Backups
sudo -u postgres pgbackrest --stanza=app --type=full --log-level-console=info backup
sudo -u postgres pgbackrest --stanza=app --type=diff --log-level-console=info backup
sudo -u postgres pgbackrest --stanza=app --type=incr --log-level-console=info backup
sudo -u postgres pgbackrest info
# Schedule (sudo -u postgres crontab -e)
# 30 01 * * 0 pgbackrest --type=full --stanza=app backup
# 30 01 * * 1-6 pgbackrest --type=diff --stanza=app backup
# Point-in-time restore into an empty test cluster on port 5433
sudo pg_createcluster 18 restore --port 5433
sudo -u postgres find /var/lib/postgresql/18/restore -mindepth 1 -delete
sudo -u postgres pgbackrest --stanza=app --pg1-path=/var/lib/postgresql/18/restore \
--type=time "--target=2026-11-18 12:49:37.647005+00" \
--target-action=promote --archive-mode=off restore
sudo systemctl start postgresql@18-restore
sudo pg_ctlcluster 18 restore stop && sudo pg_dropcluster 18 restore
# Rebuild the main cluster in place (cluster stopped), then back up again
sudo systemctl stop postgresql@18-main
sudo -u postgres pgbackrest --stanza=app --delta --log-level-console=info restore
sudo systemctl start postgresql@18-main
sudo -u postgres pgbackrest --stanza=app --type=full backup
# Monitoring
sudo -u postgres pgbackrest --output=json --stanza=app info | jq -r '.[0].backup[-1].timestamp.stop | todate'
sudo -u postgres psql -c "SELECT failed_count, last_failed_wal FROM pg_stat_archiver;"Summary and what's next
- pgBackRest is installed from PGDG and runs as the postgres user, never as root.
- /etc/pgbackrest.conf defines the repository, retention, encryption, zstd compression and the
appstanza with the exactpg1-path. - archive_command calls
pgbackrest archive-push, and archive_mode was turned on with a restart. stanza-createandcheckpassed before the first backup.- Full, differential and incremental backups run from cron, and expire removes old backups and WAL automatically.
- You restored to a point in time into a test cluster with
--archive-mode=off, and rebuilt a damaged cluster with--delta. - You check backup age and
pg_stat_archiver.failed_countevery day, and production keeps a copy of the repository off the database server.
In Part 22 we make sure these backups really work: testing backups with regular restore drills and verification.