Difficulty: beginner
Estimated Time: 20 minutes

pgBackRest

This scenario will go over the basics of using pgBackRest to perform backups and restoration. It has many, many more options available within it for customizing your backup processes, but this scenario should help you get started with it.

Full documentation is available at https://pgbackrest.org/

Let's get started!

Final Notes

We've now successfully used pgBackrest to backup and restore a database! Many, many more options are available so please read the documentation for further info. The User Guide is particularly useful for seeing various example setups - https://pgbackrest.org/user-guide-index.html

Enjoy learning about PostgreSQL? Sign up for our newsletter and get the latest tips from us each month.

pgBackRest

Step 1 of 4

Setting up PostgreSQL to use pgBackRest

pg_basebackup is included with PostgreSQL and is great for many filesystem backup scenarios. However, it can be lacking in many advanced features that enterprise backup solutions require; incrementals & differentials, retention management, parallelism, and more. pgBackRest is a third-party tool that aims to provide these advanced features and much more!

pgBackrest is available in the PGDG Yum repositories. These have already been set up in this scenario, so it's just a matter of installing the package

sudo yum install -y pgbackrest

Next some settings in the postgresql.conf must be updated to ensure WAL archiving is working. archive_mode must be turned on and the archive_command must be set to use pgBackRest's archive-push command. A stanza name for the pgBackRest repo must also be given and that will be set up in the next step. Since we're changing these values on a running system, the ALTER SYSTEM command can be used

psql -c "ALTER SYSTEM SET archive_mode = 'on'"
psql -c "ALTER SYSTEM SET archive_command = 'pgbackrest archive-push --stanza=main %p'"

Archive mode isn't typically turned on by default and changing this setting requires restarting the database to enable it

sudo systemctl restart postgresql-11

You may see errors in the postgresql logs about the archive_command failing. This is normal and should resolve itself once the pgBackRest repository is set up. Do note that until the errors subside, the database will retain all WAL files generated in the pg_wal folder until it succeeds. In the case where the pgBackRest repo may not be available for an extended period of time, the easiest thing to do is to set archive_command = '/bin/true' until then. The archive_command itself only requires a reload of the database to change, so that allows you to at least enable archive_mode until it's ready.

Next is configuring pgBackRest itself.