Sub topic 4
Choosing a dump format
This chapter assumes you can run pg_dump against a database, as covered in the previous chapter. It also assumes you know basic psql usage. Here you will compare the four archive formats that pg_dump can write and learn how each one affects restore options, speed, and file size.
The four formats
You pick a format with -F (or --format). The choice is made at dump time and cannot be changed later. It decides which tool restores the file, whether you can restore part of it, and whether the work can run in parallel.
| Flag | Name | Output | Restored with | Compression | Parallel dump | Parallel restore | Selective restore |
|---|---|---|---|---|---|---|---|
| `-Fp` | plain | One SQL script | `psql` | Only through a pipe or `-Z` | No | No | Only by editing the text |
| `-Fc` | custom | One binary archive | `pg_restore` | Built in | No | Yes | Yes |
| `-Fd` | directory | A directory of files | `pg_restore` | Built in, per file | Yes | Yes | Yes |
| `-Ft` | tar | One tar file | `pg_restore` | None | No | No | Limited |
The default is plain. The other three are archive formats. Only pg_restore can read them, and psql cannot.
pg_dump -U postgres -Fp -f mydb.sql mydb
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 mydbPlain format
A plain dump is a text file of SQL commands: CREATE TABLE, COPY ... FROM stdin blocks with the data, then indexes and constraints. You can read it, search it with grep, and edit it. The cost is that restore is all or nothing from the top of the file.
Plain dumps suit small databases, schema review in version control, and cases where you must hand the script to a person or another tool. The file is large because plain output is not compressed unless you ask for it. The usual approach is to compress through a pipe.
pg_dump -U postgres mydb | gzip > mydb.sql.gz
gunzip -c mydb.sql.gz | psql -U postgres -d mydb_restored -v ON_ERROR_STOP=1A plain restore replays every statement in order on one connection. A large database therefore restores slowly, because index builds cannot overlap.
Custom format
The custom format is a single compressed file with an internal table of contents. pg_restore uses that table of contents to restore only some objects, in a different order, or in parallel. It is a good default for one-database backups when the dump is not so large that you need parallel dumping.
pg_dump -h localhost -U postgres -Fc -f mydb.dump mydb
pg_restore --list mydb.dump | head -n 20The --list output shows each object with an ID, such as a table, a data item, an index, or a constraint. You can save that list, comment out lines you do not want, and feed it back with --use-list.
pg_restore --list mydb.dump > mydb.list
# Edit mydb.list: put ";" at the start of any line to skip that object.
createdb -U postgres mydb_partial
pg_restore -U postgres -d mydb_partial --use-list=mydb.list mydb.dumpFor simple cases, filter switches are shorter. -t orders restores one table, -n sales restores one schema, --schema-only restores definitions without rows, and --data-only restores rows without definitions.
A custom archive is one file, and pg_restore -j needs to seek inside it. Parallel restore therefore works from a file on disk but not from a pipe on standard input.
Directory format
The directory format writes a folder. It holds a toc.dat file for the table of contents plus one data file per table, compressed by default. You name the folder with -f, and pg_dump creates it. If it already exists, it must be empty.
pg_dump -U postgres -Fd -j 4 -f mydb_dir mydb
createdb -U postgres mydb_restored
pg_restore -U postgres -j 4 -d mydb_restored mydb_dirThis is the only format that supports a parallel dump. With -j 4, pg_dump opens four connections and dumps four tables at once. All workers share one snapshot, so the result is still consistent.
The speed-up has limits:
- One very large table is dumped by a single worker, so parallelism helps most when the data is spread over many tables.
- Each worker is a separate connection and consumes CPU and disk I/O on the server. Check
max_connectionsand the load on a production system first. - Workers hold locks until the dump ends. A pending
ALTER TABLEorDROPon the same tables can make the dump wait or fail, so avoid schema changes during a dump.
Tar format
The tar format writes a standard tar file that pg_restore can read. It has real drawbacks: it does not compress, it cannot restore table data in a different order, and each table's data is limited to 8 GB inside the archive. It also gives no parallel dump.
Its one advantage is that you can open the file with ordinary tar tools. You can also extract a tar archive to get a directory-format archive. For most work, custom or directory format does everything tar does without those limits. Choose tar only when something in your toolchain requires a tar file.
Compression
Archive formats compress by default. Use -Z (or --compress) to change the level or the method. A level of 0 turns compression off, and higher levels trade CPU time for size.
pg_dump -U postgres -Fd -j 4 -Z 9 -f mydb_dir mydbPostgreSQL 16 and later can choose the method as well, with values such as gzip, lz4, and zstd, written as method:level. In older versions only gzip is available. Check that your build supports the method, because lz4 and zstd depend on how PostgreSQL was compiled.
pg_dump -U postgres -Fd -j 4 --compress=zstd:5 -f mydb_dir mydbVersion and tool compatibility
Run pg_restore at the same major version as pg_dump, or newer. An older pg_restore may refuse an archive written by a newer pg_dump. Plain SQL dumps are more forgiving, but a script from a newer server may contain syntax that an older server rejects.
If you pass the wrong file to the wrong tool, you get errors like these:
psql -f mydb.dumpon a custom archive prints garbage or syntax errors, because the file is binary.pg_restore mydb.sqlon a plain script fails with "input file appears to be a text format dump. Please use psql."
Decision guide
Start from what you need to do with the dump.
In practice:
- Use custom for ordinary single-file backups of small and medium databases, where you want selective restore.
- Use directory for large databases when you want parallel dump and parallel restore, and a folder is fine for your storage.
- Use plain when readability or portability to non-PostgreSQL tooling matters more than restore flexibility.
- Use tar only when something requires it.
Whatever you choose, confirm the file is readable before you trust it. For archive formats, pg_restore --list reads the table of contents and is a quick first check. It does not prove the data restores correctly, which needs a test restore.
Exercise
You must back up a 400 GB database with hundreds of tables, and the restore must finish as fast as possible. Which format and options do you use for the dump and the restore, and why can't you use custom format for the parallel dump?
Show answer
Use directory format: pg_dump -U postgres -Fd -j 4 -f mydb_dir mydb, then createdb and pg_restore -U postgres -j 4 -d mydb_restored mydb_dir. Only the directory format supports parallel dump, because it writes separate files per table that workers can fill at once. Custom format writes one archive file, so a single process writes it. Custom format still supports parallel restore with pg_restore -j, but the dump itself would be serial.
Exercise
A colleague gives you backup.dump and runs psql -d mydb -f backup.dump. It fails. What is the likely cause, and what is the correct command?
Show answer
The file is probably a custom-format archive, which is binary, and psql can only run SQL text. Use pg_restore -U postgres -d mydb backup.dump, after creating the target database. You can confirm the format first with pg_restore --list backup.dump.