Backup and Recovery: Single-Server Streaming - Configuring PostgreSQL
Suggest editsIn this step, we'll configure a PostgreSQL server, preparing it to be backed up using Barman.
This demo is interactive
You can follow along right in your browser. When you click "Start Now," Katacoda will load a Docker Compose
environment with two container images representing a PostgreSQL 13 server with
the Pagila database loaded (named pg
)
and a backup server for Barman (named backup
).
Once you see a postgres@pg
prompt, you can follow the steps below.
For Barman to back up this server, a few things need to be done to prepare it:
- Installing the Barman CLI tools
- Creating a dedicated superuser for Barman to connect as
- Creating a dedicated streaming user with the replication attribute and remote login permissions
- Ensuring there are free replication slots
Barman CLI installation
We'll start by installing the barman-cli package: this contains the barman-wal-archive
and barman-wal-restore
commands that will be used to transmit data to and from our backup server.
Since PostgreSQL is already installed, the PostgreSQL apt repository is already configured and we can just request the package:
Further reading
For more details on this package and installation instructions for other supported platforms, see: the Barman client utilities section in the Barman guide.
User provisioning
Let's call our dedicated backup user, "barman". We'll create it interactively via the createuser
utility:
Enter example-password
when prompted (twice).
Make note of that password
We'll need to add it to the ~/.pgpass file on the backup server later!
We're making this a superuser account, which will allow it full control of all databases on this server. Be very careful with superuser credentials! Anyone who can connect as a superuser account owns your data.
Now we will create the streaming replication user, "streaming_barman". This doesn't need to be a superuser, but it does need the replication attribute:
Enter example-password
when prompted (twice).
We'll also need to edit pg_hba.conf
to allow the streaming user to connect from the backup server, by explicitly allowing it to connect in replication mode:
Enable remote access to your PostgreSQL Server
If the PostgreSQL server isn't already configured for remote access, or is restricted to connections from known machines, our database server won't be able to connect.
We'll use psql to check this setting:
The relevant configuration parameter is listen_addresses
:
The value for this server is *
- which allows connections from anything. This doesn't need to be changed. If the value were to be empty, localhost
, or a list of hosts or addresses that don't include our database server, we'd need to add its hostname (backup
) to the list, or change it to the wildcard.
Note
If you change this setting, you'll need to restart the database server for it to take effect. Since our setting was already allowing all remote connections, we don't need to do that.
Database settings for streaming
We'll also need to make sure there are replication slots available, and that PostgreSQL will allow another sender to connect. We'll continue to use psql to check the current settings:
The default for both of these (for PostgreSQL 10 and above) is 10, so we're fine - but if we needed more (or if they'd been previously set to 0, thus disabling replication) then we'd need to increase them. You could do this by using the commands ALTER SYSTEM SET max_wal_senders = 10;
and ALTER SYSTEM SET max_replication_slots = 10;
.
Note
If you change these settings, you'll need to restart the database server for them to take effect. Since our settings were already correct, we don't need to restart.
Gazing fondly at data
As we mentioned at the start, the server is preloaded with the Pagila database. This is a schema designed for examples and includes, among other things, information about actors. Before we end this step, let's query some of that data - this is what we're going to back up!
We'll verify later on that this can be restored reliably.
Continue on with Step #2: Backup Server Configuration.
Could this page be better? Report a problem or suggest an addition!