There are four main backup types which are possible when using the PostgreSQL database server.
In this section we shall discuss creating and restoring SQL dump files for both single databases and complete clusters.
As we discussed in the previous section the SQL dump method of database backup provides a simple method to produce a consistent backup of a single database or an entire cluster by generating a plain text file containing a series of SQL statements which, when executed, will recreate the database, including its data, users and permissions.
The example below shows how the pg_dump application, provided with all PostgreSQL installations, can be used to backup a single database.
If you examine the contents of the backup file we just created you will see a series of SQL statements capable of recreating the database including the table schema, the data, any indexes and sequences which we had created as well as the permissions currently assigned to our database users.
If the database is particularly large you may wish to enable compression of the resulting SQL dump file. As shown in the example below this can be easily achieved by passing the -Z n option, where n is a number between 0 and 9 with the higher number indicating higher compression, to the pg_dump utility.
The note above raises the important point that an SQL dump of a database, made with the default options, does not contain any SQL statements to recreate either the database itself or any of its users. If you are intending to restore the database to a cluster which does not already have a database with this name it is often convenient to include such commands when making the dump by passing the -C option to the pg_dump utility as shown in the following example.
All the above examples showed how to backup a single database from a cluster. While this is useful it is often desirable to create a backup of a complete cluster including all databases, tables, data, indexes and sequences, as well as any users which have been created on the cluster.
As shown in the example below the pg_dumpall application can be used to accomplish this in exactly the same way as when backing up a single database using the pg_dump application.
Current versions of the pg_dumpall do not support making compressed backups. If you wish to backup a large cluster then the output of pg_dumpall can be piped through the bzip2 utility before being redirected to a file as shown in the following example.
When creating a backup of an entire database cluster with very large tables it is often useful to break the backup into pieces of a more manageable size. The example below creates a compressed backup of a complete database cluster using the split utility to break the resulting output into files suitable for long-term archival on 700MB Compact Disks.
Now that we have explored how to create SQL dump backup files of single databases and complete database clusters we shall examine how to restore such backups to recreate a fully functioning database or cluster.
As SQL dump backup files are simply a collection of SQL statements they can be easily replayed using the psql client application provided with PostgreSQL. Assuming that the -C option was not provided then the database will have to be created first using the createdb command. The example below shows the sequence of commands required to restore such a backup.
If the -Z option was passed to the pg_dump application when creating the backup file then the zcat utility will have to be used to uncompress the backup file before piping it to the psql application. An example of using the zcat utility in conjunction with the psql application is provided below.
Finally, if the -C option was passed to the pg_dump application when creating the backup file then there is no need to create the database using createdb first as the backup file contains the appropriate SQL statements required to recreate it. As you can see from the example below a database name is still required by the psql application so we have used the internal database named postgres as it is certain to appear on all installations.
Restoring a backup of a complete database cluster from an SQL dump file is accomplished in exactly the same way as restoring a single database from a backup created with the -C option specified. As the backup file already contains the commands required to recreate databases and roles there is no need to manually recreate these first.
If a bzip2 compressed backup was created then the bzcat application can be used to uncompress the backup file before piping it to the psql application. As you can see in the example below we use the postgres internal database as the initial database as this is guaranteed to exist on all installations.
The final example in this section shows how to restore a bzip2 compressed backup of an entire database cluster which was previously broken into pieces using the split utility.
We mentioned in the previous section that user or role data was stored separately from the databases referenced by those roles and therefore it is not included in an SQL dump backup file generated by the pg_dump application. Generally, when creating a backup of a single database, that backup is expected to be restored to the same cluster. If this is the case then the absence of user metadata in the backup file is not a problem as the roles will already exist. If, however, the backup is restored to a different database cluster then the roles referenced by that backup will not already exist. For this reason it is often useful to be able to make a backup of only the user metadata.
The command in the example below will create an SQL dump file containing the SQL statements necessary to recreate all the roles present on the database cluster.
These roles can then be recreated by replaying the SQL statements from the backup file in the same way as any other SQL dump file as shown below. Alternatively the relevant commands can be extracted from the resulting file and replayed manually or added to another backup file to ensure that roles are also recreated.