Export and import PostgreSQL databases
This article describes how to back up a PostgreSQL database using pg_dump and pg_dumpall and how to restore PostgreSQL database using pg_restore.
PostgreSQL provides pg_dump and pg_dumpall tools to help you easily back up your database, and PostgreSQL provides pg_restore tool to help you easily restore your database.
Backup and recovery are must-have skills as a database administrator. PostgreSQL provides us with many convenient tools or commands to do this.
Tools or commands to backup databases:
- The
pg_dumptool is used to back up a single PostgreSQL database - The
pg_dumpalltool is used to backup all databases in the PostgreSQL server.
Tools or commands to restore databases:
- The
pg_restoretool is used to restore tar archives and catalog files produced by thepg_dumptool. - The
psqltool can import SQL script files generated by thepg_dumpandpg_dumpalltool. - The
\icommand can import SQL script files generated by thepg_dumpandpg_dumpalltools.
Back up a database using pg_dump
PostgreSQL provides the pg_dump tool for backing up a single PostgreSQL database. The following is a commonly used backup command:
pg_dump -U username -W -F t db_name > output.tar
Explanation:
-
-U username: Specify the user to connect to the PostgreSQL database server. You can use your own username at theusernamelocation. -
-W: Force prompt for password before connecting to PostgreSQL database server. After pressing Enter, you will be prompted to enter the user password. -
-F: Specify the format of the output file, which can be one of the following:c: Custom formatd: Directory archive formatt: tar archive filep: SQL script file
-
db_nameis the name of the database to be backed up. -
output.taris the path to the output file.
If you run the command in the command line or terminal tool and you are prompted that the pg_dump tool is not found, please navigate to the PostgreSQL bin folder first. E.g:
C:\>cd C:\Program Files\PostgreSQL\14\bin
Backup all databases using pg_dumpall
In addition to the pg_dump tool, PostgreSQL also provides the pg_dumpall tool for backing up all databases. The usage of the pg_dumpall tool is as follows:
pg_dumpall -U username > output.sql
Restore a database using pg_restore
PostgreSQL provides the pg_restore tool for restoring tar files and catalog files produced by the pg_dump tool.
The usage of the pg_restore tool is as follows:
pg_restore [option...] file_path
Explanation:
- The
file_pathis the path of the file or directory to restore. - The
optionare some options used when restoring data, such as database, host, port, etc. You can use options like:Option Description -a--data-onlyOnly restore data, not the table schema (data definition). -c--cleanClean up (delete) database objects before creating them. -C--createCreate the database before restoring it. -d dbname--dbname=dbnameConnect to the dbnamedatabase and restore directly into that database.-e--exit-on-errorExits if an error is encountered while sending the SQL command to the database. The default is to continue execution and display an error count at the end of recovery. -f filename--file=filenameDeclare the output file of the generated script, or the file used for listing when the -l option is present, defaults to standard output. -F format--format=formatDeclare the format of the backup file. -i--ignore-versionIgnore database version checks. -I index--index=indexOnly named indexes are restored. -l--listList the contents of the backup. The output of this operation can be limited and rearranged with the -L option to restore items. -L list-file--use-list=list-fileRestore only the elements in list-file, in the order they appear in the file. -n namespace--schema=schemaOnly restore definitions and/or data in the named schema. Not to be confused with the -s option. This option can be used with the -t option. -O--no-ownerDo not output a command that sets the object’s permissions to match the original database. -s--schema-onlyOnly the table structure (data definition) is restored. Without restoring the data, the sequence value will be reset. -S username--superuser=usernameThe username that declares the superuser when setting the close trigger. Only useful if –disable-triggers is set. -t table--table=tableRestores only the definition and/or data of the table specified by the table. -T trigger--trigger=triggerOnly the specified trigger is restored. -v--verboseDeclare redundant mode. -x--no-privileges--no-aclAvoid ACL recovery (grant/revoke commands) -X use-set-session-authorization--use-set-session-authorizationOutputs the SQL standard SET SESSION AUTHORIZATION command, not the OWNER TO command. -X disable-triggers--disable-triggersThis option is only relevant when performing restore-only data. -h host--host=hostDeclare the hostname of the machine on which the server runs. -p port--port=portDeclares the TCP port or local Unix domain socket file extension on which the server listens. -U usernameConnect as the given user. -WForce a password prompt. This should happen automatically if the server requires password authentication.
The most common usage of pg_restore is as follows:
pg_restore -d db_name path_to_db_backup_file.tar
How to restore a database using psql
You can use the psql tool to restore data from a sql file. The following is a basic usage of using psql to restore data from a sql file:
psql -U username -f path_to_db_backup_file.sql
Import the sql file using the \i command
You can also use \i command import sql files. The following demonstrates the steps to import the sakila sample database:
-
Start the psql tool and connect to the PostgreSQL server:
.\psql.exe -U postgresEnter the password for the postgres user when prompted and press Enter.
-
Create the sakila database
CREATE DATABASE sakila; -
Connect to sakila database
\c sakila; -
Use the following two statements to import the two files
postgres-sakila-schema.sqlandpostgres-sakila-insert-data.sql:\i C:/Users/Adam/Downloads/postgres-sakila-schema.sql \i C:/Users/Adam/Downloads/postgres-sakila-insert-data.sqlNote that replace
\in the file path with/.
Conclusion
This article discussed several methods for backing up and restoring PostgreSQL databases.