Backup
POSTGRESQL

Restoring a PostgreSQL Backup With pg_restore: Examples, Tips, and Tricks

intro

Let’s learn how to import a PostgreSQL database backup with the pg_restore command-line utility.

Tools used in the tutorial
Tool Description Link
PostgreSQL logo POSTGRESQL
THE POSTGRESQL DATABASE

As you may already know, pg_dump is a versatile tool for creating database backups in PostgreSQL.

If there is a tool helping you to create backups, there must also be a tool to restore these backups. This is exactly the purpose of the pg_restore command line utility!

In this guide packed with numerous examples, you will learn what pg_restore is, how it allows you to restore a PostgreSQL backup, and what options it supports.

Become a backup restoration master in PostgreSQL!

What Is pg_restore?

pg_restore is a command-line utility for restoring a PostgreSQL database from an archive in a non-plain-text format created with pg_dump. Specifically, it launches all the commands required to reconstruct a database to the state it was in at the time of the dump.

The restore process changes based on whether a database name is specified or not:

  1. A database name is specified: pg_restore connects to that database and restores the database objects read from the archive directly into the specified database.
  2. A database name is NOT specified: the process first creates a script containing the SQL commands necessary to rebuild the database. PostgreSQL executes the script, creates the database, and then imports the objects into it.

Keep in mind that pg_restore can only follow the instructions contained in the archive file generated with pg_dump. This means if the dump contains INSERT statements, the data restoration tool will not be able to load the data using COPY statements.

How to Use the PostgreSQL pg_restore Utility: Syntax and Options

The pg_restore syntax in the command line is:

Copy
        
1 pg_restore [options] [filename]

The optional parameters are:

  • options: Includes the connection information and can contain various flags to customize the behavior of the backup restore process.
  • filename: The path to the directory or archive file that contains the dump created with pg_dump. If not specified, it reads data from the standard input (stdin).

Here is a list of the most important and commonly used pg_restore options:

  • -U <username> or --username=<username>: Specifies the PostgreSQL username to connect with.
  • -h <hostname> or --host=<hostname> Specifies the host where the database server is running. If defaults to the PGHOST environment variable, if it has a value.
  • -p <port> or --port=<port> Specifies the port to use for the connection to the database. It defaults to the PGPORT environment variable if it has a value.
  • -d <dbname> or --dbname=<dbname>: Specifies the name of the database to connect to and restore data directly into. Note that in both cases, <dbname> can also be a connection string. This applies to both of the options.
  • -f <filename> or --file=<filename>: Specifies the name of the output file containing the restore process log.
  • -a or --data-only: Restores only the data, not the schema.
  • -I <index_name> or --index=<index_name>: Restores the specified index only.
  • -n <schema_name> or --schema=<schema_name>: Restores only the objects that are in the specified schema.
  • -s or --schema-only: Restores only the schema, not data.
  • -t <table_name> or --table=<table_name>: Restores only the definition and data of the specified table. In the context of this option, the concept of “table” also includes views, materialized views, sequences, and foreign tables.
  • -T <trigger_name> or --trigger=<trigger_name>: Restores only the specified trigger.
  • -1 or --single-transaction: Executes the restore as a single transaction.
  • -v or --verbose: Enables the verbose mode to log the information about the objects being restored in a verbose— more expansive —mode.
  • -c or --clean: Issues DROP commands before restoring database objects.
  • -L <list-file> or --use-list=<list-file>: Restores only those archive elements that are listed in <list-file>.
  • -l or --list: Lists the tables of contents in the dump archive. The output of this operation can be used as input to the L option.
  • -C or --create: Creates the database before loading the backup into it.
  • -e or --exit-on-error: Stops the database restoration process in case of error.
  • --no-password: Do not issue a password prompt by assuming that no password is required.

For the full list of options available, read the official documentation. Note that these options are case sensitive.

Tip: You can specify the -I, -n, and -T options multiple times to restore multiple indexes, schemas, and tables at once, respectively.

Now, take a look at the pg_restore example below:

Copy
        
1 pg_restore -U admin -d organization db_dump.tar

The above CLI command will connect to the organization database in the local PostgreSQL server. It will log in as the admin user, and you will be prompted for the user's password. Then, it will try to import the db_dump.tar archive into organization.

pg_restore Example List

Now that you know how to use the PostgreSQL restore utility, it is time to see a complete pg_restore example list!

Note: In the following CLI sample commands, the user will always be admin, the database name company, and the file name db_dump.sql. Modify these fields accordingly to make the instructions below work in your specific scenario.

Import Data Only

Copy
        
1 pg_restore -U admin -d company --data-only db_dump.sql

The company database will be filled with the data contained in db_dump.tar. To see what is going on, enable the verbose mode:

Copy
        
1 pg_restore -U admin -d company -v --data-only db_dump.sql

Import Schema Only

Copy
        
1 pg_restore -U admin -d company --schema-only db_dump.sql

This pg_restore example command only creates the schema in the company database. For a clean import, specify the -c flag:

Copy
        
1 pg_restore -U admin -d company -c --schema-only db_dump.sql

Import Only a Few Schemas

Given a PostgreSQL backup, suppose you only want to load the schemas sales and finance. You can achieve that with:

Copy
        
1 pg_restore -U admin -d company -n 'sales' -n 'finance' db_dump.sql

Note that -n can be specified multiple times in the same pg_restore command.

Import Only Some Tables

Assume you want to import only the orders table from the sales schema. Achieve that goal with:

Copy
        
1 pg_restore -U admin -d company -n 'sales' -t 'orders' db_dump.sql

Now we will get into the limitations that will dawn upon you after you start using pg_restore. The limitations are also listed in the PostgreSQL documentation, but we will summarize everything below.

Main pg_restore Limitations

This is a list of the main limitations of pg_restore:

  • None of the pg_restore options accept values including wildcards. For example, the t option of pg_dump supports wildcards but that is not true for the t flag in pg_restore.
  • When restoring data to a pre-existing table and using the --disable-triggers option, pg_restore disables the SQL triggers before inserting the data. Then, it re-enables them after the data has been imported. If the restoration process is stopped in the middle, the system catalogs might be left in the wrong state.
  • When t is specified, pg_restore does not restore any other database objects that the selected table(s) might depend on.
  • pg_restore cannot restore Large Objects selectively. Thus, it cannot restore only those for a specific table. Thus, if a dump contains Large Objects, then all large objects will be restored, or none of them if they are excluded via specific options.

Conclusion

In this guide, you understood what pg_restore is and what options it supports. This PostgreSQL command-line utility helps you restore backups generated via the pg_dump command. There are a lot of pg_restore options, and here you had the opportunity to see the most important ones with examples.

Creating and restoring database backups is essential but not easy to deal with. That is why you should opt for an advanced database client solution like DbVisualizer! In addition to supporting dozens of DBMS technologies, DbVisualizer enables you to export your databases and then import them with just a few clicks. It also provides advanced query optimization capabilities and can generate ERD-like schemas. Try DbVisualizer for free today!

FAQ

How long does pg_restore take to restore a PostgreSQL backup?

The time pg_restore takes to restore a PostgreSQL backup varies based on factors such as database size, server resources, and the restore method used. However, it usually ranges from a few minutes to several hours for large databases.

What is the path to pg_restore.exe?

On Windows, you can typically find the pg_restore.exe file in the bin folder of your PostgreSQL installation. The complete path to pg_restore.exe should be:

Copy
        
1 C:\Program Files\PostgreSQL\<version>\bin\pg_restore.exe

Replace <version> with the version number of your PostgreSQL local installation.

Is it possible to run pg_restore on a remote server?

Yes, you can run pg_restore on a remote server by following this procedure:

  1. Specify the hostname or IP address of the PostgreSQL server with the h option.
  2. Pass the PostgreSQL username to log in with the U option.
  3. Specify the name of the database where to restore data with the d option.

Make also sure that the PostgreSQL server allows remote connections. Otherwise, adjust the firewall settings. To connect to the server, you will need to provide a password or use SSH tunneling.

How to make pg_restore ignore duplicates?

By default, pg_restore restores data even if the creation command for a table fails (e.g. because it already exists). By setting the --no-data-for-failed-tables option, data for such tables will be skipped. This behavior prevents duplicate or obsolete data from being loaded into existing tables in the database.

How to perform a parallel PostgreSQL backup restore with pg_restore?

To perform a parallel PostgreSQL backup restore, pass the -j or --jobs option followed by the number of parallel jobs to pg_restore. This will run most of the most time-consuming steps of a restore, such as loading data, creating indexes, and creating constraints, concurrently. In particular, it will use up to the number of concurrent sessions passed to the option.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide to the SQL CREATE TABLE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-17
title

Clustered Indexes in PostgreSQL: What They Are and How to Use Them

author Lukas Vileikis tags DbVisualizer POSTGRESQL SQL 5 min 2025-03-13
title

SQL PIVOT: How to Use a Pivot Table In SQL

author Leslie S. Gyamfi tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-03-10
title

A Complete Guide to the SQL CREATE INDEX Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-05
title

A Complete Guide to the Order of Execution in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-03-03
title

How to Truncate Many Tables In SQL at Once

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-02-24
title

PostgreSQL Truncate Table Reset Sequence Guide

author Antonello Zanini tags POSTGRESQL 6 min 2025-02-17
title

Clustered vs Non-Clustered Index: Complete SQL Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-02-13
title

Queries and Subqueries in SQL: The Definitive Guide

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-02-12
title

SQL Derived Table: Everything You Need to Know

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-02-06

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.