$linuxjunkies
>

pg_restore(1)

Restore a PostgreSQL database from an archive file created by pg_dump.

UbuntuDebianFedoraArch

Synopsis

pg_restore [OPTION]... [FILE]

Description

pg_restore is a utility for restoring a PostgreSQL database from an archive file. It reads the archive produced by pg_dump and reconstructs the database, restoring tables, indexes, triggers, functions, and other database objects. It supports both custom binary format and tar format archives.

Unlike psql which executes SQL scripts, pg_restore works with pg_dump's special archive formats, allowing selective restoration of database objects and parallel processing for faster recovery.

Common options

FlagWhat it does
-d, --dbname=DBNAMEConnect to database DBNAME and restore directly into it
-f, --file=FILENAMEWrite output to file FILENAME instead of stdout
-j, --jobs=NUMExecute NUM restore jobs in parallel (faster for large databases)
-l, --listList the table of contents of the archive and exit
-t, --table=TABLENAMERestore only named table; can be used multiple times
-T, --exclude-table=TABLENAMEExclude specified table from restore
-s, --schema-onlyRestore only schema (no data rows)
-a, --data-onlyRestore only data, not schema objects
-c, --cleanDrop database objects before recreating them (use before restore)
-e, --exit-on-errorExit on first error instead of continuing
-h, --host=HOSTNAMEDatabase server hostname or socket directory
-U, --username=USERNAMEConnect as PostgreSQL user USERNAME

Examples

Restore archive backup.dump directly into database mydb

pg_restore -d mydb backup.dump

Convert custom format archive to SQL script and save to restore.sql

pg_restore -f restore.sql backup.dump

List the table of contents of the archive to preview what will be restored

pg_restore -l backup.dump | head -20

Restore with 4 parallel jobs, dropping existing objects first

pg_restore -d mydb -j 4 -c backup.dump

Restore only the users and orders tables from the archive

pg_restore -d mydb -t users -t orders backup.dump

Restore only the schema (tables, indexes, etc.) without data

pg_restore -d mydb -s backup.dump

Restore only data into existing schema (useful for refreshing data)

pg_restore -d mydb -a backup.dump

Restore to remote PostgreSQL server with specified user

pg_restore -h db.example.com -U postgres -d mydb backup.dump

Related commands