Take the backup with -Fc and -f, never with a shell redirect, and then restore it somewhere to prove it works:
pg_dump -h HOST -U postgres -d mydb -Fc -f mydb.dump
pg_restore -h HOST -U postgres -d mydb_check --no-owner mydb.dump
The rest of this page is about why each of those choices matters, and every claim in it was run against a live server.
Table of Contents
The failure mode that makes a backup worthless
Take the shell-redirect pattern, pg_dump ... > backup.sql, wrap it in the compression step almost everyone adds, point it at a database that is not there, and watch:
$ pg_dump -h 127.0.0.1 -p 55491 -U postgres -d hw_r6_no_such_db | gzip > backup.sql.gz
pg_dump: error: connection to server at "127.0.0.1", port 55491 failed: FATAL: database "hw_r6_no_such_db" does not exist
$ echo $?
0
$ ls -l backup.sql.gz
-rw-r--r-- 1 [user] staff 20 Aug 10 07:57 backup.sql.gz
$ gzip -dc backup.sql.gz | wc -c
0
A 20-byte archive holding nothing, and an exit status of 0. In a pipeline the shell reports the last command’s status, and gzip succeeded at compressing an empty stream. Any cron job or CI step checking $? records a successful backup.
Two fixes, and you want both. Use -f instead of a redirect, so pg_dump owns the file and its exit status:
$ pg_dump ... -d hw_r6_no_such_db -f backup4.sql
pg_dump: error: ...
$ echo $?
1
$ ls backup4.sql
ls: backup4.sql: No such file or directory
pg_dump exits 1 and leaves no misleading file behind. Where you genuinely need a pipe, set pipefail:
$ ( set -o pipefail; pg_dump ... -d hw_r6_no_such_db | gzip > backup2.sql.gz; echo "exit=$?" )
exit=1
Choosing a format
Four formats, dumping the same 5,000-customer / 20,000-order database:
pg_dump -d hw_r6_src -f plain.sql # 1,131,018 bytes (SQL text)
pg_dump -d hw_r6_src -Fc -f custom.dump # 211,249 bytes (custom, compressed)
pg_dump -d hw_r6_src -Fd -j 4 -f dirdump # 210,924 bytes (directory: toc.dat + one .dat.gz per table)
pg_dump -d hw_r6_src -Ft -f archive.tar # 1,141,760 bytes (tar, not compressed)
Compression is the least important difference. What separates them is what you can do at restore time. Plain SQL is replayed by psql from top to bottom, all of it. Custom and directory archives carry a table of contents that pg_restore can list, filter and reorder:
$ pg_restore -l custom.dump | head
;
; Archive created at 2026-08-10 07:52:33 CDT
; dbname: hw_r6_src
; TOC Entries: 22
; Compression: gzip
; Dump Version: 1.16-0
; Format: CUSTOM
; Dumped from database version: 17.10 (Homebrew)
; Dumped by pg_dump version: 17.10 (Homebrew)
That is how you restore one table out of a 400 GB dump at 2 a.m. You cannot do it from plain.sql. If you have no reason to prefer otherwise, use -Fc.
The formats are not interchangeable at restore time, and the tools say so plainly:
$ pg_restore -d somedb plain.sql
pg_restore: error: input file appears to be a text format dump. Please use psql.
The round trip, actually performed
Source database, fingerprinted:
SELECT md5(string_agg(t::text, '|' ORDER BY t.id))
FROM (SELECT id, customer_id, total FROM app.orders) t;
hw_r6_src: customers=5000 orders=20000 fp=b9ed2e32d94707f23c32c79f3783afbd
Dumped with -Fc, restored into an empty database, fingerprinted again:
$ pg_restore -d hw_r6_restored --no-owner custom.dump
$ # same query against the restored database
hw_r6_restored: customers=5000 orders=20000 fp=b9ed2e32d94707f23c32c79f3783afbd
Identical. The foreign key, the sequence default and the view came across too. The directory dump restored into a second, separate cluster with pg_restore -j 4 produced the same fingerprint. backup-database-with-pg-dump.sh dumps a database from a cluster of its own, restores it into a second, separate cluster, compares md5 fingerprints of the two copies and asserts the gzip pipeline’s exit status 0 and the missing-role error on restore; any database you already run is left alone.
pg_dump does not back up your roles
This is the second way a dump that looked fine fails when you need it. pg_dump covers one database. Roles, passwords and tablespaces are cluster-wide, and it emits the GRANT without the role it refers to:
$ grep -c "CREATE ROLE" withrole.sql
0
$ grep -n "hw_r6_app" withrole.sql
25199:GRANT SELECT ON TABLE app.customers TO hw_r6_app;
Restored into a fresh cluster that has never heard of that role:
$ psql -d hw_r6_target -f withrole.sql
psql:withrole.sql:25199: ERROR: role "hw_r6_app" does not exist
$ echo $?
0
Exit status 0, again. psql reports failure per statement and success overall unless you tell it otherwise:
$ psql -d hw_r6_target3 -v ON_ERROR_STOP=1 -f withrole.sql
$ echo $?
3
pg_restore is better behaved — it exits 1 after pg_restore: warning: errors ignored on restore: 1 — but only stops at the first error if you pass --exit-on-error.
The globals come from a second command, which you run once per cluster:
pg_dumpall -h HOST -U postgres --globals-only -f globals.sql
Restore globals.sql first, then the per-database dumps. Note what globals.sql contains:
16:CREATE ROLE hw_r6_app;
17:ALTER ROLE hw_r6_app WITH ... LOGIN ... PASSWORD 'SCRAM-SHA-256$4096:WZ0Ink...';
Password verifiers, in a plain file. They are SCRAM hashes rather than plaintext, but they are sufficient to authenticate as that role against any server that accepts them. Treat globals.sql as a secret: chmod 600, and never commit it.
Schema only, data only
pg_dump -d hw_r6_src --schema-only -f schema.sql # 3,560 bytes: 2 CREATE TABLE, 0 COPY
pg_dump -d hw_r6_src --data-only -f data.sql # 1,128,152 bytes: 0 CREATE TABLE, 2 COPY
--data-only is the sharp one: it does not disable triggers or defer constraints by default, so loading it into a populated database can fail on foreign key ordering. It is for refilling a schema you just created, not for merging data into a live one. If you emptied that schema by dropping and recreating it, check its grants before you restore into it — a public schema you create by hand does not come back with the privileges initdb gave it.
Parallel dumps need the directory format
$ pg_dump -d hw_r6_src -Fc -j 4 -f bad.dump
pg_dump: error: parallel backup only supported by the directory format
$ pg_dump -d hw_r6_src -j 4 -f bad2.sql
pg_dump: error: parallel backup only supported by the directory format
Only -Fd accepts -j, because it is the only format where several processes can write at once — PostgreSQL’s documentation gives exactly that reason. pg_restore accepts -j for both custom and directory archives. The test database here is about a megabyte, far too small for parallelism to show a meaningful gain, so no speedup figure is quoted on this page; measure it on your own data before assuming -j helps.
Version skew: dump with the newer binary
Only PostgreSQL 17.10 is installed on the machine used for this article, so this is the one section below that is cited rather than executed. From the pg_dump documentation:
“Because pg_dump is used to transfer data to newer versions of PostgreSQL, the output of pg_dump can be expected to load into PostgreSQL server versions newer than pg_dump’s version. pg_dump can also dump from PostgreSQL servers older than its own version. (Currently, servers back to version 9.2 are supported.) However, pg_dump cannot dump from PostgreSQL servers newer than its own major version; it will refuse to even try, rather than risk making an invalid dump.”
So when the versions differ, run the newer pg_dump. Upgrading 15 to 17 means using the 17 client for both halves. The archive header shown earlier records both versions (Dumped from database version and Dumped by pg_dump version), which is the fastest way to check what produced a file you inherited.
One edit to the output above: the account name in every command result on this page has been replaced with [user]. Nothing else is altered — the commands, their output, the permissions, the timings and the errors are exactly as they ran.