December 4, 2026

pg_dump and pg_restore: Backing Up a PostgreSQL Database, and the Restore That Fails on a New Server

A database server with an external backup drive and a glowing activity light

A backup made with pg_dump restored cleanly into a new database on the same server: 200,000 orders in, 200,000 orders out. The same file restored onto a freshly installed second server finished with a warning that one error had been ignored. The data was all there. A permission was not, because the role it belonged to had never been backed up. That gap, and the format choices that decide how flexible a restore can be, are what this post works through. Every command was run against two PostgreSQL 16.15 clusters set up for the test, with a sample database of 2,000 customers and 200,000 orders.

Video: WittCode on YouTube

What pg_dump does

A consistent snapshot of one database

The PostgreSQL 16 documentation describes pg_dump as making consistent backups even if the database is being used concurrently, without blocking other users who are reading or writing. That makes it safe to run against a live application. The dump reflects the database as of the moment it started.

The same page sets its scope: pg_dump only dumps a single database. Global objects shared by every database in a cluster, such as roles and tablespaces, are out of reach. For those, the documentation points to pg_dumpall.

The basic command

pg_dump shop > shop.sql

That produces a plain SQL script: CREATE TABLE statements, the data as COPY blocks, then indexes, constraints and permissions. It is readable, searchable with grep, and restorable with psql.

External hard drive

Choosing a format

Plain, custom or directory

pg_dump has three formats worth knowing. The test dumped the same database each way:

Format Command Size
Plain SQL pg_dump shop > shop.sql 9,469,935 bytes
Plain SQL, gzipped pg_dump shop \| gzip > shop.sql.gz 961,509 bytes
Custom pg_dump -Fc shop -f shop.dump 947,480 bytes
Directory, 4 jobs pg_dump -Fd -j 4 shop -f shop.dir 946,259 bytes

The custom format came out at about a tenth of the plain file without any extra step. The documentation explains why: it is compressed by default. It also calls custom and directory the most flexible formats, because they allow manual selection and reordering of archived items during restore. That flexibility is the real reason to choose them, as the restore tests show.

Parallel dumps

The -j option runs the dump in parallel, dumping several tables at once. The documentation notes that it works only with the directory format, because that is the only format where multiple processes can write their data at the same time, and that pg_dump will open one connection per job plus one, so max_connections has to allow for it. On a database this small the timing difference was nil, about 0.13 to 0.14 seconds for every format. On a database with many large tables, parallel jobs are where the time savings live.

Restoring

Plain dumps go through psql

A plain SQL file is just a script:

createdb shop_plain
psql -v ON_ERROR_STOP=1 shop_plain -f shop.sql

ON_ERROR_STOP=1 makes psql stop at the first error instead of reporting it and carrying on, which is what a careful restore wants. The test’s count afterward: 200,000 orders.

Custom and directory dumps go through pg_restore

createdb shop_c
pg_restore -d shop_c -j 4 shop.dump

pg_restore can also run in parallel with -j, and it reads the archive’s table of contents to restore objects in a safe order. Again, 200,000 orders.

Restoring over an existing database

Run the same pg_restore a second time into the same database and it fails item by item:

pg_restore: error: could not execute query: ERROR:  relation "customers" already exists
pg_restore: error: could not execute query: ERROR:  relation "customers_id_seq" already exists
...
pg_restore: warning: errors ignored on restore: 10

By default, pg_restore reports errors and continues, which is how a restore can “finish” with nothing actually replaced. Adding --clean --if-exists drops each object before recreating it; the test then completed without errors and the order count was still 200,000. The --create option goes further. The pg_restore documentation says it will create the database before restoring into it, and with --clean it drops and recreates the target database first. In the test, pg_restore -C -d postgres shop.dump created a database named shop from the archive’s own information.

Filing cabinet

Restoring only part of a dump

Reading the table of contents

A custom archive can be listed without restoring anything. pg_restore -l prints its table of contents, which the documentation says can also be used as input to the -L option. A filtered extract from the test archive:

3405; 0 16386 TABLE DATA public customers postgres
3407; 0 16395 TABLE DATA public orders postgres
3257; 1259 16407 INDEX public orders_customer_id_idx postgres
3415; 0 0 ACL public TABLE orders postgres

Saving that list to a file, deleting or commenting out lines, and passing it back with -L restores exactly the selected items in the listed order.

One table

For the common case, -t restores the definition and data of only the named table. The documentation notes that “table” here also covers views, materialized views, sequences and foreign tables, and that -t can be repeated. Into an empty database, pg_restore -d shop_one -t customers shop.dump produced one table holding 2,000 rows, and nothing else.

That is the practical argument for the custom format. When someone deletes the wrong rows from one table, restoring that table from last night’s archive into a scratch database takes one command. With a plain SQL file, it takes a text editor and patience.

Smaller and narrower dumps

Structure without data

pg_dump -s dumps only the object definitions, which the documentation describes as the schema without data. For the test database that came to 3,363 bytes containing both CREATE TABLE statements, against 9.4 MB for the full plain dump. A schema-only dump is useful for comparing two environments or for seeding a test database with the right shape and no customer records.

Leaving one big table empty

Real databases often have one table, a log or an event history, that dwarfs everything else and is not needed in a copy. --exclude-table-data tells pg_dump not to dump data for tables matching a pattern, while still dumping their definitions. With --exclude-table-data=orders, the custom archive shrank from 947,480 bytes to 13,675. Restored, the orders table existed with its columns and indexes, and held zero rows.

The snapshot in practice

The consistency promise was easy to see. While a loop inserted 50 new orders into the live database, pg_dump -Fc ran against it. The restored copy held exactly 200,000 orders; the live database ended with 200,050. The dump captured the database as it stood when the dump began and ignored what arrived during it, with no locks that stopped the inserts.

Close-up of a key chain

The part pg_dump leaves out

Roles

The test database granted SELECT on orders to a role named reporter. That grant is in the dump: grep found GRANT SELECT ON TABLE public.orders TO reporter; in the plain file. The role itself is not, because roles belong to the cluster, not the database.

On a second, freshly initialized cluster, restoring the same archive produced:

pg_restore: error: could not execute query: ERROR:  role "reporter" does not exist
pg_restore: warning: errors ignored on restore: 1

Every table and row arrived. The permission did not. On a real migration, that is the kind of failure that shows up a week later as a reporting job that can no longer read its data.

pg_dumpall –globals-only

The fix is the companion tool the pg_dump documentation points to. pg_dumpall --globals-only writes roles and tablespaces without any table data; on the test cluster its output included CREATE ROLE reporter;. Restoring that file first with psql, then the database archive, finished with no errors on the fresh server. (It also printed one harmless error, that role postgres already existed, because every new cluster starts with that role.)

A backup routine that restores

What to keep

For a single-database application, the tests point to a short routine:

  • A nightly pg_dump -Fc of each database, kept with dated filenames.
  • A pg_dumpall --globals-only alongside it, so roles and their passwords travel with the data.
  • A restore test into a scratch database on a schedule, counting rows in the tables that matter. A dump file that has never been restored is a hope, not a backup. The PGDMP signature at the start of a custom archive, and a readable pg_restore -l listing, are quick sanity checks that a file is intact; only a real restore proves the data inside it is usable.

Logical dumps are not the only kind of PostgreSQL backup. Continuous archiving and physical base backups, covered elsewhere in the PostgreSQL documentation, allow recovery to a point in time, which a nightly dump cannot. For many small and medium databases, a tested nightly dump is the backup that actually exists.

Sources

  • pg_dump, PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/app-pgdump.html
  • pg_restore, PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/app-pgrestore.html

670 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *