pg_basebackup

On this page 7

pg_basebackup is the command-line utility shipped with PostgreSQL that takes a physical base backup of an entire running cluster by streaming its data files over the replication protocol. In database Backup & recovery practice, it is the standard built-in tool for producing the starting point of point-in-time recovery and for seeding new streaming replication standbys, all without stopping the server.

Unlike a logical export, the result is a byte-level copy of the data directory plus the write-ahead log (WAL) needed to make that copy consistent. It therefore restores only to the same major PostgreSQL version and platform architecture, but it restores far faster than replaying SQL for large databases.

How pg_basebackup copies a live cluster

pg_basebackup connects to the server as a replication client, asks it to start a non-exclusive backup, and receives every file of the cluster as a stream while normal reads and writes continue. Because pages change during the copy, the files on their own are fuzzy and inconsistent; consistency is restored later by replaying the WAL generated between the backup start and end points.

The server first performs a checkpoint, records the starting WAL location, and then sends the base directory and each tablespace. When the transfer ends, it writes the stop location and a backup_label file that tells recovery where replay must begin. Since PostgreSQL 13, it also produces a backup manifest listing every file with its size and checksum.

The following table summarizes the server-side prerequisites, which are the most common reason a first attempt fails.

RequirementTypical settingWhy it matters
REPLICATION role attributeDedicated backup roleGrants access to the replication protocol
pg_hba.conf entryDatabase field set to replicationPermits replication connections from the backup host
max_wal_sendersAt least 2 spare sendersStreaming WAL uses a second connection
wal_levelreplica or logicalMinimal level lacks the WAL needed for recovery
Replication slotOptional, via -S or -CKeeps the primary from recycling needed WAL
Server requirements for running pg_basebackup

Key options and output formats

The behavior of pg_basebackup is shaped mainly by its output format, its WAL method and its checkpoint mode. Getting these three choices right decides whether the backup is self-contained, how much space it needs and how much load it places on the primary.

OptionValuesEffect
-F (format)p (plain) or t (tar)Plain mirrors the data directory; tar writes one archive per tablespace
-X (WAL method)stream, fetch or noneStream is the default and makes the backup self-contained
-c (checkpoint)spread or fastFast starts sooner but causes an input and output spike
-R (write recovery config)FlagCreates standby.signal and primary_conninfo for a replica
-Z (compression)gzip, lz4 or zstd, client or serverReduces size; server-side compression saves network bandwidth
-r (max rate)For example 100MThrottles transfer to protect production traffic
-T (tablespace mapping)old_dir=new_dirRelocates tablespaces in plain format
Frequently used pg_basebackup options

With -X stream, a second connection receives WAL in parallel with the data files, so the backup contains everything needed for a consistent restore even if WAL archiving is not configured. With -X none, the backup is usable only together with a working WAL archive, which is acceptable when archiving is already in place for point-in-time recovery.

PostgreSQL 17 added incremental backups: with --incremental pointing to the manifest of an earlier backup and summarize_wal enabled on the server, only changed blocks are copied. The chain is later merged into a full data directory with pg_combinebackup, while pg_verifybackup checks any backup against its manifest.

pg_basebackup occupies the physical, cluster-wide corner of the PostgreSQL toolset. It complements, rather than replaces, logical tools such as pg_dump and full backup managers such as pgBackRest, and the table below shows where each fits.

ToolBackup typeGranularityMain strength
pg_basebackupPhysical, hotWhole cluster onlyBuilt in, simple, seeds replicas
pg_dumpLogicalSingle database, schema or tablePortable across versions and platforms
pgBackRestPhysical, hotWhole clusterParallelism, retention, encryption, repositories
File system snapshotPhysical, crash-consistentVolumeNear-instant capture on supported storage
How pg_basebackup differs from neighboring tools

The decisive difference from pg_dump is that a base backup cannot restore a single table and cannot cross major versions, yet it supports WAL replay to any moment after the backup. Compared with pgBackRest, pg_basebackup offers no built-in retention policy, catalog or parallel transfer, so larger estates often outgrow it.

Using pg_basebackup in a recovery strategy

A sound strategy treats pg_basebackup output as the anchor of a recovery chain: a periodic base backup plus continuous WAL archiving allows restoring to any timestamp, transaction or named restore point within the retention window. Without archived WAL, each backup restores only to the moment it finished.

The right configuration depends on the goal, as the decision table shows.

GoalRecommended settingsReason
Build a new standby-F p -X stream -R -C -S slot_nameReady-to-start replica that does not lose WAL
Nightly self-contained backup-F t -X stream -Z zstdCompact archive restorable without the WAL archive
Point-in-time recovery base-F t -X none with WAL archivingArchive already holds the WAL; avoids duplication
Busy production primary-c spread -r limit, or run on a standbyLimits impact on latency and disk throughput
Choosing pg_basebackup settings by goal

Running pg_basebackup against a standby offloads the read work from the primary and is fully supported. In that case, the standby must be receiving WAL steadily, because the backup cannot complete until the required WAL has been replayed or streamed.

Limitations and common mistakes

Most failed restores trace back to untested assumptions, not to the tool itself. The following mistakes recur in practice:

  • Taking backups with -X none while WAL archiving is broken, which produces a backup that cannot reach consistency.
  • Never running a test restore, so a missing tablespace or permission problem appears only during an outage.
  • Creating a replication slot for a one-time backup and forgetting to drop it, which makes the primary retain WAL indefinitely and fill its disk.
  • Expecting to restore one database or table; pg_basebackup copies the whole cluster and nothing smaller.
  • Ignoring the backup manifest; running pg_verifybackup after each backup catches corruption in transit or storage.

Throughput is also a real constraint: the copy is single-threaded per connection, so multi-terabyte clusters may take many hours. At that scale, incremental backups, storage snapshots or a parallel tool usually shorten both backup and restore windows.

In simple terms

pg_basebackup makes a complete copy of a PostgreSQL database server while it keeps working. Think of photographing a busy office: people keep moving while the camera pans, so the picture is a little blurred. PostgreSQL fixes the blur by also saving its change journal, which lets the copy be corrected to one exact moment.

The copy is not a readable list of commands but the actual files the server uses. That makes it fast to restore and ideal for starting a second, mirrored server, but it only works with the same PostgreSQL version and always contains everything on the server at once.

A practical example

An operations team runs a 600 GB PostgreSQL 16 cluster for an order-management system and needs a second server for failover. From the new host, an engineer runs pg_basebackup with plain format, -X stream, -R and a newly created replication slot, throttled with -r 200M so customer traffic is not affected.

The copy completes in about an hour. Because -R wrote standby.signal and the connection settings, the engineer simply starts PostgreSQL on the new host, and it connects to the primary and begins streaming within seconds. The team also schedules a weekly compressed tar backup with WAL archiving, verifies each one with pg_verifybackup, and restores it monthly to a test machine, giving them a proven point-in-time recovery path alongside the live replica.