RefLib

Reference library for AI

Back

postgresql_16:4.11.1.1 Restoring the Dump

PostgreSQL 16 > Server Administration > Backup and Restore > SQL Dump > Restoring the Dump

Text files created by pg_dump are intended to be read by the psql program using its default settings. The general command form to restore a text dump is psql -X *dbname* < *dumpfile* where *dumpfile* is the file output by the pg_dump command. The database *dbname* will not be created by this command, so you must create it yourself from template0 before executing psql (e.g., with createdb -T template0 *dbname*). To ensure psql runs with its default settings, use the -X (--no-psqlrc) option. psql supports options similar to pg_dump for specifying the database server to connect to and the user name to use. See the (see postgresql_16:7.2.18) reference page for more information.

Non-text file dumps should be restored using the (see postgresql_16:7.2.16) utility.

Before restoring an SQL dump, all the users who own objects or were granted permissions on objects in the dumped database must already exist. If they do not, the restore will fail to recreate the objects with the original ownership and/or permissions. (Sometimes this is what you want, but usually it is not.)

By default, the psql script will continue to execute after an SQL error is encountered. You might wish to run psql with the ON_ERROR_STOP variable set to alter that behavior and have psql exit with an exit status of 3 if an SQL error occurs: psql -X --set ON_ERROR_STOP=on *dbname* < *dumpfile* Either way, you will only have a partially restored database. Alternatively, you can specify that the whole dump should be restored as a single transaction, so the restore is either fully completed or fully rolled back. This mode can be specified by passing the -1 or --single-transaction command-line options to psql. When using this mode, be aware that even a minor error can rollback a restore that has already run for many hours. However, that might still be preferable to manually cleaning up a complex database after a partially restored dump.

The ability of pg_dump and psql to write to or read from pipes makes it possible to dump a database directly from one server to another, for example: pg_dump -h *host1* *dbname* | psql -X -h *host2* *dbname*

Important

The dumps produced by pg_dump are relative to template0. This means that any languages, procedures, etc. added via template1 will also be dumped by pg_dump. As a result, when restoring, if you are using a customized template1, you must create the empty database from template0, as in the example above.

After restoring a backup, it is wise to run ANALYZE (see postgresql_16:7.1.44) on each database so the query optimizer has useful statistics; see (see postgresql_16:4.10.1.3) and (see postgresql_16:4.10.1.6) for more information. For more advice on how to load large amounts of data into PostgreSQL efficiently, refer to (see postgresql_16:3.11.4).

Back