Sub topic 4
Choosing a PostgreSQL Dump Format
This chapter assumes basic SQL and psql usage, Linux command-line familiarity, and a working knowledge of PostgreSQL roles, databases, and transactions. It focuses on the format of a logical dump, not on creating the dump itself or designing a complete backup system.
A dump format determines how PostgreSQL stores the exported database and which tool restores it. The main choices are plain SQL, custom, directory, and tar.
Compare the formats
| Format | Created with | Restored with | Main strength | Main limitation |
|---|---|---|---|---|
| Plain SQL | `pg_dump` without `-F` | `psql` | Human-readable and widely portable | Limited restore selection and no parallel restore |
| Custom | `pg_dump -Fc` | `pg_restore` | Selective and ordered restoration | Not directly readable as SQL |
| Directory | `pg_dump -Fd` | `pg_restore` | Parallel dump and restore | Produces a directory tree, not one file |
| Tar | `pg_dump -Ft` | `pg_restore` | One archive file with archive-style restore | Cannot be restored in parallel |
Plain SQL is a sequence of SQL statements. You can inspect it with a text editor, send it to psql, or modify it before restoring. This makes it useful for small databases, migrations, and environments where simple text files are preferred.
Custom format is a PostgreSQL archive. It stores the dump in a form that pg_restore can inspect and replay. You can restore selected schemas or tables, change the restore order, and exclude objects such as ownership commands.
Directory format stores the dump as multiple files in a directory. It is the only pg_dump format that supports parallel dump jobs with -j. pg_restore can also read the directory and restore several objects at the same time.
Tar format stores an archive in one tar file. It supports the archive features provided by pg_restore, but it does not support parallel dumping or restoring.
Choose based on the restore you need
Use plain SQL when readability and simple tooling matter more than selective restoration. Use custom format as the general default for a single-file backup that may need flexible recovery.
Use directory format when the database is large and dump or restore time matters. It requires enough storage for the directory and careful handling of the complete directory tree. Copying only part of that tree produces an unusable or incomplete archive.
Use tar when a single archive file is convenient and parallelism is not required. A tar archive is not the same as a filesystem backup. It contains logical database objects, not the PostgreSQL data directory.
pg_dump -U postgres -Fc -f mydb.dump mydb
pg_dump -U postgres -Fd -j 4 -f mydb_dir mydb
pg_dump -U postgres -Ft -f mydb.tar mydb
pg_dump -U postgres -f mydb.sql mydbThe format is fixed when the dump is created. You cannot pass a custom-format archive to psql as if it were SQL text. Likewise, pg_restore is designed for archive formats and is not the normal tool for a plain SQL file.
Understand archive inspection
Archive formats support a table of contents. Inspect it before a full restore to confirm that the archive is readable and to see its object list.
pg_restore --list mydb.dump | head -n 20You can use the table of contents to plan a selective restore. For example, pg_restore can restore only a named schema, skip ownership commands, or write SQL output instead of applying changes directly. These options make custom and directory formats better suited to controlled recovery and testing.
The format choice does not replace backup verification. A command can finish successfully while the resulting file is missing, inaccessible, or unsuitable for the intended restore. Keep the complete output, record the PostgreSQL version and command options, and test restoration on another database or server.
Exercise
You need a single archive file, selective restoration, and no parallel restore. Which format should you choose, and which tool restores it?
Show answer
Choose custom format with pg_dump -Fc. Restore it with pg_restore.