The pg_dump and pg_restore command line tools are used to export and import Post­gr­eSQL databases. They create Post­gr­eSQL backups and migrate Post­gr­eSQL databases between servers.

What is a Post­gr­eSQL dump?

A Post­gr­eSQL dump is the output file which is created when exporting a Post­gr­eSQL database. Post­gr­eSQL is a soph­ist­ic­ated database man­age­ment system which stores data in optimised data struc­tures. Therefore, ex­tract­ing struc­tured data from a Post­gr­eSQL database requires a special procedure.

Note

What is a backup?. We answer this basic question in our dedicated guide.

The pg_dump tool creates a text file with SQL commands, similar to MySQL backup with MySQL dump. Running the commands will restore the database to the time of the dump. According to the official Post­gr­eSQL doc­u­ment­a­tion:

Quote

“The idea behind this dump method is to generate a text file with SQL commands that, when fed back to the server, will recreate the database in the same state as it was at the time of the dump.”

It is important to un­der­stand what `Post­gr­eSQL database´ means. The term is often confused with Post­gr­eSQL server. In fact, it is not uncommon for a single Post­gr­eSQL server to contain multiple databases. Below is an overview of the hierarchy of objects in a Post­gr­eSQL in­stall­a­tion:

Post­gr­eSQL Object Contains
Server Databases
Database Tables
Table Records
Record Fields

How do pg_dump and pg_restore work?

The command line tools pg_dump and pg_restore are usually installed with the Post­gr­eSQL client ap­plic­a­tions, as well as the Post­gr­eSQL command line interface psql. The tools follow the Unix philo­sophy and use text streams for input and output. This allows them to be linked to other programs through pipes. Input and output can also be read from files or output to files using re­dir­ec­tions.

We show the general pattern when using pg_dump to create a Post­gr­eSQL dump:

pg_dump dbname > db.dump

We use a re­dir­ec­tion of the output (“>”) to a file. The Post­gr­eSQL dump generated from this contains SQL commands. These can be run with the psql tool. We’ll show the general pattern when using psql to read a Post­gr­eSQL dump:

psql dbname < db.dump

As you can see, the command is similar to the command for pg_dump. However, the input (“<”) from the Post­gr­eSQL dump file must be re­dir­ec­ted.

The pg_dump tool can do a lot more. It is also possible to output databases in special dump formats. However, the pg_restore or psql tool is needed to restore them depending on the format of the generated Post­gr­eSQL dump.

Below you will find an overview of the command line tools used for creating and restoring Post­gr­eSQL backups:

Tool Ex­plan­a­tion
pg_dump Command line tool to create a Post­gr­eSQL dump
pg_restore Command line tool to restore a Post­gr­eSQL database from a Post­gr­eSQL dump; allows special op­er­a­tions like partial imports, re-sorting of import data, parallel import of multiple tables, etc.
psql Post­gr­eSQL command line interface; accepts SQL commands from the command line or from a Post­gr­eSQL dump file and runs them

Let’s take a look at the pattern when using pg_restore for a Post­gr­eSQL dump:

pg_restore --dbname=dbname db.dump

The Post­gr­eSQL dump must be created in a special format for pg_restore. Below are the possible output formats for pg_dump:

pg_dump output format Ex­plan­a­tion Import via
Plain Plain text file with SQL commands; com­press­ing requires an ad­di­tion­al tool, such as Gzip psql
Custom Com­pressed dump format; import can be con­trolled in detail pg_restore
Directory Creates directory with one file per table/blob; table of contents; can be edited with standard Unix tools; allows parallel export of multiple tables
Tar Archiving format ‘Tape Archive’; can be converted to directory format; com­press­ing requires ad­di­tion­al tool, such as Gzip; not possible to control sequence of imports pg_restore

Finally, the pattern for pg_dump used when creating a Post­gr­eSQL dump in a `custom´ dump format is:

pg_dump --format=custom dbname > db.dump
Tip

If your Post­gr­eSQL in­stall­a­tion is running in a Docker container, you can use pg_dump inside the container to create a Post­gr­eSQL backup. You can also save the entire container as Docker backup. We explain how to do this in detail in our article

Step by step guide: Create and restore a Post­gr­eSQL backup

There are several different ways to create and restore Post­gr­eSQL backups. The methods offer different ad­vant­ages and dis­ad­vant­ages, so it depends on the de­ploy­ment scenario and re­quire­ments. The procedure used when creating the Post­gr­eSQL dump will determine which method should be used during import.

A quick note before we get into the specific methods of creating and restoring Post­gr­eSQL dumps; The examples below simply reference the name of the database used. However, they do not include database usernames or passwords. These are stored in the .pgpass password file following the Post­gr­eSQL con­ven­tion. This file can be found in the user’s home directory and contains Post­gr­eSQL con­nec­tion data. The following format is used:

hostname:port:database:username:password

The in­form­a­tion contained in the password file is auto­mat­ic­ally used when the command line tools are enabled. This elim­in­ates the risk of entering any sensitive data on the command line.

Check if the tools are available and install them if necessary

Firstly, you must check if pg_dump and pg_restore are installed. Try to enable the tools to display their version. If this fails, then the tool is not on your system and must be installed.

  • Verify that pg_dump is installed:
pg_dump --version
  • Verify that pg_restore is installed:
pg_restore --version
  • Fur­ther­more, check if the Post­gr­eSQL command line interface psql is installed:
psql --version

The tools can be easily installed if they are not found.

  • Install the Post­gr­eSQL client ap­plic­a­tions using Homebrew on Mac:
brew install libpq
brew link --force libpq
  • Use the built-in package man­age­ment under Ubuntu-Linux:
sudo apt-get install postgresql-client
  • Follow our in­struc­tions to install Post­gr­eSQL on Windows Server 2016 on Windows.

Create and restore Post­gr­eSQL backup

Firstly, let’s look at the simplest way to create a Post­gr­eSQL backup. We extract a single database from a Post­gr­eSQL server. The structure and contents of the database are written on a file in SQL commands. Enabling the pg_dump tool will look like this:

pg_dump dbname > db.dump

But what if you want to restore the Post­gr­eSQL dump to another server? The Post­gr­eSQL command line interface psql is used. The command is very simple:

psql dbname < db.dump

The pg_dump tool allows users to create spe­cial­ised Post­gr­eSQL dump formats, other than the output of a Post­gr­eSQL dump as a text file with SQL commands. These are con­trolled through options when they are enabled. Below is an overview of the two most useful dump formats:

Post­gr­eSQL dump format Detailed options syntax Short options syntax
Custom pg_dump --format=custom pg_dump -Fc
Directory pg_dump --format=directory pg_dump -Fd

Now, Post­gr­eSQL dump in a custom format must be created:

pg_dump --format=custom dbname > db.dump

Use the pg_restore tool to restore to another server:

pg_restore --dbname=dbname db.dump

An ad­di­tion­al step is required to restore the Post­gr­eSQL dump to the same server, as the database and tables already exist on the server and must be removed before importing. This is similar to '--add-drop-tables' for MySQL dump. Post­gr­eSQL con­veni­ently allows you to add the func­tion­al­ity during import:

pg_restore --clean --create --dbname=dbname db.dump

The '—clean' option removes the existing database before the import. The '--create' option creates the database under the specified name. This allows the import to run without problems.

Migrate Post­gr­eSQL database between remote servers

Export and import of a Post­gr­eSQL dump can be connected with a pipe (‘|’). This exports the data directly into the import. It is possible to import the export from one server directly to another server, since pg_dump, pg_restore, and psql operate on a remote host when needed. Let’s take a look at the command used for this. Use the '—host' option to specify the host names:

pg_dump --host=export_host dbname | psql --host=import_host dbname

Create Post­gr­eSQL backup of large databases

Post­gr­eSQL is a pro­fes­sion­al database man­age­ment system. A special procedure is needed to create backups of large databases, as Post­gr­eSQL dumps can be very large. Firstly, com­pres­sion is advised. Post­gr­eSQL dumps exported as text files usually contain large amounts of redundant SQL commands which can be easily com­pressed.

Pipe the output of pg_dump to the Gzip com­pres­sion tool and write it as a com­pressed .gz file:

pg_dump dbname | gzip > db.dump.gz

Reverse the process to recover from a com­pressed Post­gr­eSQL dump. The gunzip tool unpacks the com­pressed data and outputs it to standard output. Pipe the output to the psql tool and use the '-c' option to ensure the input file is un­af­fected when unpacking:

gunzip -c db.dump.gz | psql dbname

The 3 2 1 backup rule requires at least one backup in the cloud. Uploading very large files can be prob­lem­at­ic under certain cir­cum­stances. It might make sense to split the Post­gr­eSQL dump into multiple files using the split tool. A pipe should be used again to forward the output of pg_dump to split. We split the Post­gr­eSQL dump into in­di­vidu­al files with a maximum size of 1 GB in the example below:

pg_dump dbname | split -b 1G - db.dump

How can the partial files created by split during import be merged again? No special software is needed for this, as the standard cat tool can be used. We forward a list of partial Post­gr­eSQL dumps, which have been combined by the cat tool into a coherent data stream. Pipe this to psql as usual:

cat db.dump* | psql dbname

It is possible to dump multiple tables in parallel when using the directory dump format. This is faster, but it also leads to a higher load on the database server. We control the number of tables exported in parallel using the '—jobs' option. In our example, we export three tables in parallel. It is not possible to redirect the output to a file as we are writing a directory. We use the '—file' option instead with spe­cific­a­tion for the directory name:

pg_dump --jobs=3 --format=directory --file=dump.dir dbname
Go to Main Menu