What are the pg_dump output formats and which should you use?
IntermediateConceptPostgreSQL
Answer
Short answer
pg_dump can write plain SQL (-Fp), custom (-Fc), directory (-Fd) or tar (-Ft). Custom format is the usual choice: compressed, and pg_restore can restore selectively and in parallel. Directory format adds parallel dumping for large databases.
Comparison
| Format | Flag | Restore with | Compressed | Parallel dump | Selective restore |
|---|---|---|---|---|---|
| Plain SQL | -Fp (default) |
psql -f |
Optional (pipe to gzip) | No | No (edit the file) |
| Custom | -Fc |
pg_restore |
Yes | No | Yes |
| Directory | -Fd |
pg_restore |
Yes | Yes (-j) |
Yes |
| Tar | -Ft |
pg_restore |
No | No | Yes |
Examples
pg_dump -Fc -U postgres shop -f shop.dump
pg_restore -l shop.dump # list contents
pg_restore -d shop_new -t orders shop.dump # restore one table
pg_dump -Fd -j 8 -U postgres bigdb -f /backups/bigdb # 8 parallel jobs
pg_restore -j 8 -d bigdb_new /backups/bigdb
What to say in the interview
"Custom format for most backups — compressed with selective restore through pg_restore. Directory format with -j for big databases to dump and restore in parallel. Plain SQL when I need a human-readable file."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow is PostgreSQL organised: cluster, database, schema and table?BeginnerHow do you connect to PostgreSQL from the command line?Beginner