Sub topic 3
Dumping a Single Database with `pg_dump`
Prerequisites and the backup boundary
This chapter assumes basic SQL and psql usage, familiarity with PostgreSQL roles and databases, and comfort with the Linux command line. You should also understand that a transaction is a group of changes committed as one unit.
pg_dump creates a logical backup of one PostgreSQL database. A logical backup contains database objects such as tables, indexes, functions, and their data as SQL statements or a PostgreSQL-specific archive. It does not copy the server's data directory.
A dump normally includes:
- Schemas, tables, sequences, views, functions, and indexes
- Table data
- Database object ownership and privilege commands
- Large objects, unless excluded
It does not include cluster-wide objects such as login roles or tablespace definitions. Back those up separately with pg_dumpall --globals-only.
Create a custom-format dump
The custom format is usually a good default because pg_restore can select individual objects, reorder operations, and restore in parallel later.
pg_dump -h localhost -U postgres -Fc -f mydb.dump mydbThis command connects to the mydb database on localhost as postgres and writes the compressed archive to mydb.dump.
The command may prompt for a password unless authentication is configured through a password file, a local trust rule, or another supported method. Avoid putting passwords directly in shell history.
The dump is taken from a consistent database snapshot. Applications can continue using the database while pg_dump runs. The dump represents a transactionally consistent view, although it does not provide a recovery history for changes made after the dump began.
Choose what to include
Use options to create a smaller or specialized dump.
# Objects and definitions, but no table rows
pg_dump -U postgres --schema-only -f mydb_schema.sql mydb
# Table rows, but no object definitions
pg_dump -U postgres --data-only -Fc -f mydb_data.dump mydb
# Only one schema
pg_dump -U postgres -Fc -n reporting -f reporting.dump mydb
# Only selected tables
pg_dump -U postgres -Fc -t public.customers -t public.orders \
-f sales_tables.dump mydbA table filter can omit dependencies that the selected table needs. For example, a filtered dump may not include every function, type, or extension required by the table. Treat filtered dumps as targeted migration or recovery tools, not always as complete database backups.
The most useful connection and output options are:
| Option | Purpose |
|---|---|
| `-h`, `-p`, `-U` | Select the host, port, and database role |
| `-F` | Select the format, such as `p`, `c`, `d`, or `t` |
| `-f` | Choose the output file or directory |
| `-v` | Show detailed progress |
| `--no-owner` | Omit ownership commands during a compatible restore |
-F p creates plain SQL, which you restore with psql. -F c creates custom format, and -F d creates directory format. Directory format is required when using parallel dump jobs.
See the backup flow
Common errors
- Running
pg_dumpagainst the wrong host or database. Confirm the connection before creating a production backup. - Assuming roles are included. They are cluster-wide and require a separate globals backup.
- Writing the dump into a directory without enough free space.
- Ignoring warnings or a nonzero exit status.
- Treating a filtered dump as a complete disaster-recovery backup.
Exercise
Which command creates a selectively restorable custom-format dump of the database mydb?
Show answer
pg_dump -U postgres -Fc -f mydb.dump mydb