Sub topic 9
Taking a Physical Base Backup
This chapter assumes you know basic Linux commands, PostgreSQL roles, the data directory, and the idea of a transaction. A physical base backup copies the database cluster at the file level. It includes every database in that cluster, system catalogs, roles, and other files stored in the data directory.
When to Use a Physical Base Backup
A physical backup is useful when you need to restore the whole PostgreSQL cluster, not just one database. It preserves the internal files PostgreSQL uses to store tables, indexes, and transaction state.
A physical backup has important limits:
- It normally restores the entire cluster, not one table or database.
- It works with the same PostgreSQL major version and compatible platform.
- It does not automatically include configuration files stored outside the data directory.
- It needs archived or streamed WAL to support point-in-time recovery.
- It is not a portable export for moving selected objects between PostgreSQL versions.
The recovery point objective (RPO) determines how much recent data you can lose. The recovery time objective (RTO) determines how quickly the server must return to service. A base backup gives you a starting state. WAL archiving supplies changes made after that state.
| Need | Suitable approach |
|---|---|
| Restore one database or selected objects | Logical dump with `pg_dump` |
| Restore the complete cluster | Physical base backup |
| Recover to a time before an error | Base backup plus archived WAL |
Preparing the Server
pg_basebackup connects to a running PostgreSQL server and copies its files consistently. The connection must use a role with the REPLICATION attribute, or a role allowed by the server's replication configuration.
For a production backup, check these items first:
- The backup destination has enough space.
- The destination is on separate storage from the database.
- The backup role is allowed in
pg_hba.confwith a replication connection rule. - The server has an appropriate
wal_level, normallyreplicaor higher. - The backup directory is empty or does not yet exist.
- PostgreSQL client tools match the server version where practical.
A dedicated replication role limits access to the backup operation:
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'use-a-secret-from-a-secure-store';Avoid putting a real password directly in shell history. Use a protected password file or another approved secret-management method.
Running `pg_basebackup`
This command creates compressed tar files, streams the WAL generated during the backup, and prints progress:
pg_basebackup -h localhost -U replicator -D /backups/base -Ft -z -Xs -PThe options mean:
-h localhostselects the server host.-U replicatorselects the replication role.-D /backups/baseselects the destination.-F tchooses tar format.-zcompresses the tar files.-X sstreams required WAL through the backup connection.-Pdisplays progress.
The result usually includes a base backup archive and a WAL archive. Store the output somewhere that survives failure of the database host. Copying the backup to another disk on the same host protects against some mistakes, but not host loss.
> callout note > -Xs protects consistency during the backup by including the WAL needed for recovery. It does not replace continuous WAL archiving. Archive WAL separately when you need recovery beyond the backup's completion point.
Checking and Restoring the Backup
Check that the expected tar files exist and that the command completed successfully. A successful copy is not proof that the backup can be restored. Test it on a separate PostgreSQL instance.
To restore, stop the target server, empty its data directory, extract the base archive there, extract any WAL archive into the appropriate location, and restore required configuration files. Set ownership and permissions to the PostgreSQL operating-system user before starting the server.
A base backup also copies cluster-wide roles because they are stored inside the cluster. External services, extension packages, certificates, and operating-system settings still need separate recovery plans.
Exercise
Does a physical base backup let you restore only one table without restoring the cluster?
Show answer
No. It represents the complete cluster. Use a logical dump when you need selected databases or objects.