Course contentsShow
← Databases & SQL
Lesson 961 of 1,9538. PostgreSQL InternalsFree lesson

Backup Strategies: Physical vs Logical

You will compare physical backups and logical dumps to design a recovery strategy for your data.

In this lesson, you will learn how to choose between physical and logical backups to build a robust recovery strategy for your PostgreSQL instance.

When securing your data, you must choose between backing up the files that make up your database or the SQL commands needed to reconstruct it.

Physical Backups (The Snapshot Approach)

Physical backups involve copying the actual data files from the PGDATA directory while the database is running (often using tools like pg_basebackup). Because these are bit-for-bit copies of the underlying storage, they are incredibly fast to restore.

  • Pros: Restores are nearly instantaneous regardless of database size, and you preserve the exact physical state, including internal indexes and free space maps.
  • Cons: You generally cannot perform a "selective" restore of a single table. You must restore the entire cluster. Furthermore, they are tied to specific major versions of PostgreSQL and the underlying hardware architecture (Endianness).

Logical Dumps (The SQL Approach)

Logical backups use tools like pg_dump to query the database and export the data into a text-based format (SQL commands).

  • Pros: They are highly portable. You can restore a logical dump from an older version of PostgreSQL into a newer one, or even move it across different operating systems. They allow for granular recovery—you can restore just one specific table or schema if a user accidentally deletes data.
  • Cons: Restoring a large logical dump is slow. The database must re-index every table, enforce constraints, and calculate statistics from scratch during the import process.

Designing Your Strategy

For high-availability environments—which we’ve touched on previously through tools like Patroni—physical backups are your primary defense against total server failure. They allow for fast Point-in-Time Recovery (PITR) when paired with your Write-Ahead Logs (WAL).

However, keep a logical dump in your toolkit for "oops" moments. If an application developer accidentally drops a critical table, a logical dump allows you to extract that specific object without overwriting the entire production database.

Key Takeaway: Use physical backups for rapid, large-scale disaster recovery, and use logical dumps for cross-version portability and granular, table-level object restoration.