Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
POSTGRESQL DATABASES
Options for backing up and restoring your PostgreSQL databases.
PostgreSQL provides different ways to backup and restore your databases. With PostgreSQL,
backups can be full, incremental or continuous, and they can be at logical or filesystem level.
Point-in-time recovery is possible from incremental backups. PostgreSQL even supports a
feature called timelines, which is a sort of branching history of your backup and restores.
Let’s have a look at the common options for backup and restore.
The output generated by pg_dump is not a traditional “backup”. It omits some information
that makes it unusable for some operations, like for example initializing a standby server.
The output is also much bigger than from other backup methods, making it suitable only for
“small” databases.
Note that we’re dumping only objects from the public schema. This is typically what you
want.
The file mydb.sql is a plain text file with PostgreSQL commands, and the file mydb.pgdmp is
a custom format file. Note that the custom format file is gzip-compressed and it is not
required to compress it again.
The PostgreSQL docs have more info about all the options for pg_dump and pg_dumpall.
The SQL file of course, can be sourced in the usual way with psql to recreate the database(s).
However, there are a few options that you probably want to specify so that the execution
goes through cleanly – see the second example below. Of these, the -1 option ensures that
the whole script is executed in a single transaction, so that you have a all-or-nothing restore.
Let’s say someone accidently dropped a table, and you’d like to restore only that table.
Restoring from a custom format pg_dump file is the easiest way to do this.
You can use the pg_restore utility to restore a full custom format dump file, but it’s real
value lies in the ease of importing a single function, table or trigger from the dump file.
The pg_basebackup makes a replication protocol connection (just like a replication client) to
the PostgreSQL server, and creates a binary copy of the data files that live in
the $PGDATA directory of the server. The copy it creates is consistent – the files exactly
correspond to the state at the end of some particular transaction.
This also implies that pg_basebackup needs to connect as a user who is explicitly permitted
to use the replication protocol. You can do this by adding lines to pg_hba.conf similar to:
If you are worried about missing transactions that happen while the backup is going on, you
can ask pg_basebackup to fetch and include those transaction log files (WAL files) also, using
the -x or -X options.
$ pg_basebackup -x -D /path/to/datadir
STREAMING REPLICATION
If you can afford the extra resources, having an up-to-date hot standby server, continuously
replicating from your primary server, is a great way to mitigate downtime risk. You also get
a “free” server to run your reports and other analytics, without loading your primary.
Learn how you can use the streaming replication feature to do this in our All About
PostgreSQL Streaming Replication blog post.
Note that it is possible to take backups of any type from standby servers also.
You can read more about WAL archiving in this All about WAL archiving in PostgreSQL blog
post.
Typically, full backups are taken periodically along with continuous WAL archiving.
Together, these allow for point-in-time recovery.
For example, let’s say something drastic happened at 11:20 AM and you’d like to restore the
databases to the state it was just before 11:20. Assume you take daily backups at 01:00 AM
each day and use continuous WAL archiving, you can follow these steps:
stop postgres
restore the last full backup, the one made at 01:00 AM
mount the filesystem with the WAL archive files
create a $PGDATA/recovery.conf file that has the
contents:
recovery_target_inclusive = false