3.6. Backing up and restoring PostgreSQL data with logical dumps
A logical backup of PostgreSQL data consists of the SQL statements necessary to restore the data. The advantage of logical backup over physical backup is that the data can be restored on other hardware configurations and PostgreSQL versions.
The SQL dump method is based on generating a dump file with SQL commands. When a dump is uploaded back to the database server, it recreates the database in the same state as it was at the time of the dump.
The SQL dump is ensured by the following PostgreSQL client applications:
-
pg_dumpdumps a single database without cluster-wide information about roles or tablespaces -
pg_dumpalldumps each database in a given cluster and preserves cluster-wide data, such as role and tablespace definitions.
By default, the pg_dump and pg_dumpall commands write their results into the standard output. To store the dump in a file, redirect the output to an SQL file. The resulting SQL file can be either in a text format or in other formats that allow for parallelism and for more detailed control of object restoration.
You can perform the SQL dump from any remote host that has access to the database.
3.6.1. Advantages and disadvantages of an SQL dump 复制链接链接已复制到粘贴板!
SQL dumps are text files containing a database’s structure and data in the form of SQL statements.
Advantages:
-
An SQL dump is the only PostgreSQL backup method that is not server version-specific. The output of the
pg_dumputility can be reloaded into later versions of PostgreSQL, which is not possible for file system level backups or continuous archiving. - An SQL dump is the only method that works when transferring a database to a different machine architecture, such as going from a 32-bit to a 64-bit server.
-
An SQL dump provides internally consistent dumps. A dump represents a snapshot of the database at the time
pg_dumpbegan running. -
The
pg_dumputility does not block other operations on the database when it is running.
Disadvantage:
- An SQL dump takes more time compared to a file system level backup.
You can create a backup of a single PostgreSQL database by using the pg_dump utility to export the database structure and data to a file.
Prerequisites
-
You are logged in as the
postgressuperuser or a user with database administrator privileges.
Procedure
Dump a database without cluster-wide information:
$ pg_dump <db_name> > <dump_file>To specify which database server
pg_dumpwill contact, use the following command-line options:The
-hoption to define the host.The default host is either the local host or what is specified by the
PGHOSTenvironment variable.The
-poption to define the port.The default port is indicated by the
PGPORTenvironment variable or the compiled-in default.
You can restore a PostgreSQL database from an SQL dump file by using the pg_restore utility to re-create the database structure and data.
Prerequisites
-
You are logged in as the
postgressuperuser or a user with database administrator privileges.
Procedure
Create a new database:
$ createdb <db_name>- Verify that all users who own objects or were granted permissions on objects in the dumped database already exist. If such users do not exist, the restore fails to re-create the objects with the original ownership and permissions.
Run the
psqlutility to restore a text file dump created by thepg_dumputility:$ psql <db_name> < <dump_file>where
<dump_file>is the output of thepg_dumpcommand. To restore a non-text file dump, use thepg_restoreutility instead:$ pg_restore <non-plain_text_file>
You can create a backup of all databases on a PostgreSQL server by using the pg_dumpall utility to export all databases and cluster-wide data to a single file.
Prerequisites
-
You are logged in as the
postgressuperuser or a user with database administrator privileges.
Procedure
Dump all databases in the database cluster and preserve cluster-wide data:
$ pg_dumpall > <dump_file>To specify which database server pg_dumpall will contact, use the following command-line options:
The
-hoption to define the host.The default host is either the local host or what is specified by the
PGHOSTenvironment variable.The
-poption to define the port.The default port is indicated by the
PGPORTenvironment variable or the compiled-in default.The
-loption to define the default database.This option enables you to choose a default database different from the
postgresdatabase created automatically during initialization.
You can restore all databases on a PostgreSQL server from a pg_dumpall file by using the psql utility to re-create the entire database cluster.
Prerequisites
-
You are logged in as the
postgressuperuser or a user with database administrator privileges.
Procedure
- Ensure that all users who own objects or were granted permissions on objects in the dumped databases already exist. If such users do not exist, the restore fails to re-create the objects with the original ownership and permissions.
Run the
psqlutility to restore a text file dump created by thepg_dumpallutility:$ psql < <dump_file>where
<dump_file>is the output of thepg_dumpallcommand.
3.6.6. Handling SQL errors during restore 复制链接链接已复制到粘贴板!
By default, the psql utility continues to execute if an SQL error occurs, causing the database to restore only partially. Alternatively, you can configure psql to stop on errors to ensure data integrity.
Prerequisites
-
You are logged in as the
postgressuperuser or a user with database administrator privileges.
Procedure
Make
psqlexit with an exit status of 3 if an SQL error occurs by setting theON_ERROR_STOPvariable:$ psql --set ON_ERROR_STOP=on <db_name> < <dump_file>Specify that the whole dump is restored as a single transaction so that the restore is either fully completed or canceled.
When restoring a text file dump by using the
psqlutility:$ psql -1When restoring a non-text file dump by using the
pg_restoreutility:$ pg_restore -e
Note that when you use this approach, even a minor error can cancel a restore operation that has already run for many hours.