Sub topic 2
Logical versus Physical Backups
This chapter assumes basic SQL and psql usage, familiarity with Linux commands, PostgreSQL roles and databases, and a basic understanding of transactions. It also assumes that you can identify the PostgreSQL data directory and connect to a server with suitable privileges.
PostgreSQL backups fall into two main families:
- A logical backup stores database objects and their data as statements or PostgreSQL dump records.
- A physical backup stores the database cluster's files, usually together with the write-ahead log (WAL) needed for recovery.
The right choice depends on what must be recovered and how quickly it must happen.
Start with recovery goals
A recovery point objective (RPO) is the maximum amount of data you can afford to lose. An RPO of one hour might be acceptable for a reporting database. An RPO close to zero may be required for an order system.
A recovery time objective (RTO) is the maximum acceptable time before the service is available again. Logical dumps can take time to restore, while a physical backup can usually bring back an entire cluster more directly.
Consider the failure scenario:
| Failure scenario | Useful backup approach | Why |
|---|---|---|
| Recover one dropped table | Logical dump | Restore selected objects without replacing the whole cluster |
| Rebuild an entire server | Physical base backup | Restore the cluster files and continue recovery |
| Recover data just before an accidental delete | Physical backup plus WAL archiving | Replay WAL to a chosen time |
| Move selected tables to another database | Logical dump | Export and restore individual objects |
Logical backups
pg_dump connects to one database and reads its schema and data through PostgreSQL. It does not copy the server's data files. A dump can be restored into a different database, and some dump formats allow you to select individual tables or schemas.
A plain SQL dump contains commands such as CREATE TABLE and COPY. Custom and directory formats contain PostgreSQL-specific records that pg_restore can inspect and replay.
pg_dump -h localhost -U postgres -Fc -f mydb.dump mydb
pg_restore --list mydb.dump | head -n 20Logical backups are useful when you need portability, selective restoration, or migration between compatible PostgreSQL versions. They are usually slower to create and restore than physical backups for a large database. A logical dump also does not include roles, tablespace definitions, or server configuration unless you back up those items separately.
pg_dump captures a transaction-consistent view of the database. Changes committed while the dump runs are handled according to that consistent snapshot.
Physical backups
A physical backup copies the files that make up a PostgreSQL cluster. A cluster is the server-managed storage area that contains one or more databases, shared roles, and other system data. pg_basebackup is the standard PostgreSQL command for taking a physical base backup.
pg_basebackup -h localhost -U replicator -D /backups/base -Ft -z -Xs -PPhysical backups are suited to disaster recovery and whole-cluster recovery. They preserve the internal representation of the database, so they are tied to the PostgreSQL major version and the target environment. You cannot normally use a physical backup to restore only one table.
A base backup becomes more powerful when combined with continuous WAL archiving. WAL records changes made after the base backup. Replaying those records can recover the cluster to a selected time, a process called point-in-time recovery (PITR).
Choosing between them
Use logical backups when portability and object-level recovery matter. Use physical backups when rapid whole-cluster recovery and PITR matter. Production systems commonly use both: scheduled logical dumps for portability and selective recovery, plus physical backups and WAL archiving for disaster recovery.
Exercise
Exercise
You need to restore one table from yesterday's backup and also recover the entire cluster after a disk failure. Which backup family should support each task?
Show answer
Use a logical backup for the individual table and a physical base backup with archived WAL for whole-cluster recovery.