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.
| Requirement | Typical setting | Why it matters |
|---|---|---|
| REPLICATION role attribute | Dedicated backup role | Grants access to the replication protocol |
| pg_hba.conf entry | Database field set to replication | Permits replication connections from the backup host |
| max_wal_senders | At least 2 spare senders | Streaming WAL uses a second connection |
| wal_level | replica or logical | Minimal level lacks the WAL needed for recovery |
| Replication slot | Optional, via -S or -C | Keeps the primary from recycling needed WAL |
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.
| Option | Values | Effect |
|---|---|---|
| -F (format) | p (plain) or t (tar) | Plain mirrors the data directory; tar writes one archive per tablespace |
| -X (WAL method) | stream, fetch or none | Stream is the default and makes the backup self-contained |
| -c (checkpoint) | spread or fast | Fast starts sooner but causes an input and output spike |
| -R (write recovery config) | Flag | Creates standby.signal and primary_conninfo for a replica |
| -Z (compression) | gzip, lz4 or zstd, client or server | Reduces size; server-side compression saves network bandwidth |
| -r (max rate) | For example 100M | Throttles transfer to protect production traffic |
| -T (tablespace mapping) | old_dir=new_dir | Relocates tablespaces in plain format |
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 compared with related backup tools
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.
| Tool | Backup type | Granularity | Main strength |
|---|---|---|---|
| pg_basebackup | Physical, hot | Whole cluster only | Built in, simple, seeds replicas |
| pg_dump | Logical | Single database, schema or table | Portable across versions and platforms |
| pgBackRest | Physical, hot | Whole cluster | Parallelism, retention, encryption, repositories |
| File system snapshot | Physical, crash-consistent | Volume | Near-instant capture on supported storage |
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.
| Goal | Recommended settings | Reason |
|---|---|---|
| Build a new standby | -F p -X stream -R -C -S slot_name | Ready-to-start replica that does not lose WAL |
| Nightly self-contained backup | -F t -X stream -Z zstd | Compact archive restorable without the WAL archive |
| Point-in-time recovery base | -F t -X none with WAL archiving | Archive already holds the WAL; avoids duplication |
| Busy production primary | -c spread -r limit, or run on a standby | Limits impact on latency and disk throughput |
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.