pgBackRest

On this page 7

pgBackRest is an open-source backup and restore tool for PostgreSQL that takes physical backups of a database cluster, archives its write-ahead log and restores the cluster to any consistent moment covered by that archive. In the field of Backup & recovery within Databases, it is one of the most widely deployed answers to a hard question: how to protect a multi-terabyte PostgreSQL cluster with predictable recovery time and verifiable integrity.

Unlike a logical tool such as pg_dump, pgBackRest copies data files at the block and file level, so a restore produces an exact physical replica of the original cluster. Its design centers on parallel processing, multiple backup types, repository encryption and cloud object storage, which makes it a common choice for production fleets rather than single small databases.

How pgBackRest protects a PostgreSQL cluster

pgBackRest works by combining periodic physical backups with continuous WAL archiving. A backup captures the data directory while the server keeps running, and the archived write-ahead log records every change made after that backup started. Replaying the log on top of a restored backup brings the cluster to a consistent state or to a chosen recovery target.

The central configuration unit is the stanza, which describes one PostgreSQL cluster: its data directory, port, host and the repositories that hold its backups. Every command, from stanza-create to restore, names the stanza it acts on. PostgreSQL hands each completed WAL segment to pgBackRest through the archive_command setting, typically pgbackrest --stanza=main archive-push %p, and the archive-get command fetches segments back during recovery.

Each backup records a manifest listing every file, its size, its timestamp and a SHA-1 checksum. The manifest allows later backups to detect what changed, lets restores verify every file, and makes it possible to rebuild only damaged or modified files with a delta restore.

The main commands and their roles are summarized below.

CommandPurposeTypical use
stanza-createInitializes repository storage for a clusterOnce, during setup
checkConfirms configuration and that WAL archiving worksAfter setup and after changes
backupTakes a full, differential or incremental backupScheduled, for example nightly
archive-push / archive-getStores and retrieves WAL segmentsCalled by PostgreSQL automatically
restoreRebuilds the data directory from a backupRecovery, cloning, replica builds
expireRemoves backups and WAL beyond retentionRuns after each backup
verifyChecks repository files against checksumsPeriodic integrity audits
Core pgBackRest commands

Backup types and retention in pgBackRest

pgBackRest offers three backup types that trade backup time and storage against restore complexity. A full backup copies the whole cluster, a differential backup copies files changed since the last full backup, and an incremental backup copies files changed since the most recent backup of any type. Every restore still needs the full backup at the root of the chain.

Backup typeWhat it copiesDepends onTrade-off
FullEvery file in the clusterNothingSlowest and largest, simplest to restore
DifferentialChanges since the last fullOne full backupGrows over the week, short chain
IncrementalChanges since the last backupFull plus prior backupsSmallest, longest dependency chain
Comparison of pgBackRest backup types

By default change detection works at file level using size and timestamp, with an optional checksum-based mode for stricter detection. Recent releases add block incremental backups, which store only changed blocks inside large table files and reduce storage for databases with large, sparsely updated tables.

Retention is set per repository, for example repo1-retention-full=2 keeps two full backups and everything that depends on them. The expire command then removes older backups and the WAL they no longer need. Retention therefore defines the recovery window: point-in-time recovery is possible only back to the oldest retained backup.

Repositories, performance and security features

A repository is the storage location where pgBackRest keeps backups and archived WAL. It can be a local or mounted filesystem, a dedicated backup host reached over SSH or the TLS-based pgBackRest server, or object storage such as Amazon S3, Azure Blob Storage, Google Cloud Storage and S3-compatible services. Up to four repositories can be configured per stanza, which supports a local copy for fast restores alongside an offsite cloud copy for disaster recovery.

Performance comes from parallel worker processes set by process-max, which compress, checksum and transfer files concurrently. Compression supports gzip, lz4, zstd and bzip2; lz4 favors speed, while zstd usually gives a better ratio at moderate CPU cost. Asynchronous archiving queues WAL pushes in the background so that a busy primary does not stall when the archive target is slow.

For confidentiality, repositories can use client-side AES-256-CBC encryption with a passphrase, so backups remain unreadable even if the bucket is exposed. pgBackRest also validates PostgreSQL page checksums during backup when they are enabled, which surfaces silent storage corruption before it spreads into every retained backup.

The following settings have the greatest influence on behavior.

SettingTypical valueWhat it affects
process-max2 to 16Parallelism and CPU load on the source host
compress-typelz4 or zstBackup speed versus repository size
repo1-retention-full2 to 4Recovery window and storage cost
archive-asyncyWAL throughput on write-heavy primaries
repo1-cipher-typeaes-256-cbcEncryption of stored backups
backup-standbyyOffloads file reads to a replica
Key pgBackRest settings and their effect

pgBackRest belongs to the physical backup family and fills the gap between the built-in pg_basebackup and enterprise suites. pg_dump produces a logical backup of a single database that is portable across versions but cannot support point-in-time recovery. pg_basebackup takes a full physical copy, and from PostgreSQL 17 it can also take incremental backups, but it leaves retention, repositories and WAL management to the operator.

ToolBackup levelPoint-in-time recoveryBuilt-in retention
pgBackRestPhysical, full/diff/incrYes, with integrated WAL archiveYes
pg_basebackupPhysical, full or incrementalYes, with separate archivingNo
pg_dumpLogical, per databaseNoNo
BarmanPhysical, full and incrementalYes, with WAL streamingYes
pgBackRest and neighboring backup tools

The practical difference is operational completeness: pgBackRest manages archiving, retention, verification and multi-repository storage in one tool, while simpler utilities require scripts around them. Teams that only need a nightly copy of a small database may find pg_dump sufficient, but strict recovery point objectives usually call for a physical tool with continuous archiving.

Restoring with pgBackRest and common mistakes

A restore stops PostgreSQL, rebuilds the data directory from the selected backup and writes the recovery settings so that the server replays archived WAL on startup. The --type=time option with --target selects a timestamp for point-in-time recovery, while --delta rewrites only files whose checksums differ, which can cut restore time dramatically on a large cluster that is mostly intact.

Most failures come from configuration and process, not from the tool itself. The table below lists frequent problems.

SymptomLikely causeFix
Backup waits for WAL and times outarchive_command not set or failingRun the check command and read the PostgreSQL log
Recovery window shorter than expectedRetention too lowRaise full retention or add a second repository
Restore much slower than backupSingle process or remote repositoryRaise process-max and keep a local repository
Backups unusable after key lossEncryption passphrase not stored safelyKeep the passphrase in a separate secrets vault
Untested backupsNo restore drillsSchedule regular test restores to a scratch host
Frequent pgBackRest problems and fixes

The most important rule is that a backup is proven only by a restore. Regular restore drills, the verify command and monitoring of archive lag together turn pgBackRest from a copying tool into a dependable recovery process.

Put simply

pgBackRest is a safety system for PostgreSQL databases. It regularly takes a complete copy of the database files and, in between, keeps a running diary of every change. If something goes wrong, it puts back the last copy and replays the diary up to the moment just before the problem.

Think of a photographer who saves a full photo album every Sunday and writes down every edit made during the week. Losing the album on Thursday is not a disaster, because Sunday’s copy plus the notes rebuild Thursday morning exactly. The key point is simple: backups only count once a restore has been tested.

In practice

An online retailer runs a 4 TB PostgreSQL cluster with one primary and two replicas. The database team configures pgBackRest with a weekly full backup on Sunday, daily differential backups and asynchronous WAL archiving. Repository one sits on a local backup server for fast restores, and repository two is an encrypted S3 bucket in another region with four weeks of retention. Backups read files from a replica to spare the primary.

One afternoon a faulty deployment deletes most rows from the orders table at 14:32. The team restores the cluster to a separate host with a time target of 14:31 and uses a delta restore on a recent clone, so only changed files are rewritten. Within about 90 minutes the missing orders are exported from the recovered copy and reinserted into production. The outcome is no lost orders and a clear audit trail of exactly which data was recovered.