← Blog
An ivory tape cartridge with a plum reel and orange latch.
Published on
2026-11-25

Use pg_basebackup to restore a PostgreSQL cluster

Your replacement PostgreSQL server starts. Does it have the orders database, the role your reporting job uses and the orders that were committed when the backup finished? A physical backup gives you a way to answer all three in one restore drill.

We took a full backup of a running PostgreSQL 18.6 cluster, added a third order to the source, then started a copy of the backup on a separate port. The restored database contained the two earlier orders and their original total. Here’s how to run that check and how to tell which parts of recovery you have verified.

Choose the recovery unit first

In PostgreSQL, a cluster means the databases managed by one server instance. pg_basebackup copies that entire cluster, including shared roles. It suits a replacement instance with a compatible platform and the same major PostgreSQL version. If you need one database, selected tables or a migration between major versions, start with the pg_dump and pg_restore guide.

For this drill we used one data directory with no external tablespaces, no replication upstream and no external archiving commands. The database dispatch held two orders worth 1,250 and 3,750 cents. The cluster also had a role named reporting. Write down expected values like these before you take the backup. They give you something specific to check once the restored server accepts connections.

Take a full backup while the source is running

Use PostgreSQL 18 tools for this example. Set SOURCE_HOST, SOURCE_PORT and BACKUP_USER for the source. The account needs replication access, a matching replication entry in pg_hba.conf and an authentication method such as a protected password file. Streaming the write-ahead log (WAL) alongside the backup uses a second replication connection, so check that the server has two available WAL senders. Set BACKUP to a dedicated new or empty destination with room for the cluster.

pg_basebackup -h "$SOURCE_HOST" -p "$SOURCE_PORT" -U "$BACKUP_USER" \
  -D "$BACKUP" -Fp -X stream --manifest-checksums=SHA256
pg_verifybackup "$BACKUP"

-Fp writes a plain directory. -X stream includes the WAL needed to bring this backup to a consistent state. The manifest records SHA-256 file checksums with the option shown above. Check that the backup command succeeds before running the verifier. PostgreSQL documents the connections and tablespace options in its pg_basebackup reference.

What does the manifest catch?

The intact backup passed pg_verifybackup. On a separate disposable copy, we changed one byte in the orders table’s data file while keeping its size the same. The verifier reported a checksum mismatch and exited with status 1. That is a reason to reject that copy. Keep the original backup untouched for the startup test.

A successful verification still leaves an operational question: can the replacement server use the files? The verifier’s checks have a defined scope. PostgreSQL recommends performing a test restore too. Read what pg_verifybackup checks.

Start an isolated copy and check the application data

The shell example assumes the simple layout described above. Run it as the database service account, with RESTORE set to a path that does not yet exist, RESTORE_LOG to a writable log path and RESTORE_PORT to an unused port. For a production-derived backup, isolate the test host from production services, review configuration and extension dependencies, and remap external tablespaces before startup. A different port alone does not isolate outbound connections or filesystem writes.

cp -a "$BACKUP" "$RESTORE"
pg_ctl -D "$RESTORE" -l "$RESTORE_LOG" \
  -o "-h 127.0.0.1 -p $RESTORE_PORT" -w start

Connect to dispatch on the restore port. Check the port in the first result before trusting the next two. Accidentally querying the source can make an unsuccessful recovery look convincing.

SELECT current_database(), inet_server_port();
SELECT count(*) AS orders, sum(cents) AS total_cents FROM orders;
SELECT rolname FROM pg_roles WHERE rolname = 'reporting';

Our source had three orders totaling 5,900 cents after the later transaction. The recovered instance had two totaling 5,000 cents, and the role query returned reporting. The 900-cent order was committed after the backup completed. Recovering it would require WAL covering that later commit and a recovery procedure that replays it. The continuous archiving guide explains point-in-time recovery. Shut down the test copy when you finish.

pg_ctl -D "$RESTORE" -m fast -w stop

To size a recovery host on a CPU instance from Compute with Hivenet, time a representative restore from the place you actually store your backups. Include transfer, WAL replay, startup and the first successful application check. Our two-order drill verifies recovery behavior. Your database size and storage path determine the time you need to budget.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.