Set Up PostgreSQL 9.5 Master-Slave Replication Using Repmgr Edward Samuel's Blog
Set Up PostgreSQL 9.5 Master-Slave Replication Using Repmgr Edward Samuel's Blog
Replication is a process to create redundant resources those are consistent each other. The goal of replication is either to improve reliability,
accessibility or fault-tolerance. There are some ways to do database replication:
In this article, I will explain how to set up master-slave replication using repmgr. repmgr can create a full database replication and be a hot
standby (or streaming replicated) server.
Here, I will set up a single master node and a single slave replication. The slave database can be used as read replica. I will use Ubuntu 14.04 and
PostgreSQL 9.5. Some parts of the documentation here are taken from the repmgr documentation with some additional missing steps.
Step 1: Install PostgreSQL 9.5 and repmgr to master and slave servers
In order to get the latest update for PostgreSQL, we need to add PostgreSQL Apt Repository and then install the database.
POSTGRES_VERSION=9.5
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" | sudo tee -a /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get -y update
sudo apt-get -y install postgresql-${POSTGRES_VERSION} postgresql-${POSTGRES_VERSION}-repmgr
Adjust scp commands so it will copy the key pairs into all your database cluster nodes, which is master and slave.
1
listen_addresses = '*'
2
wal_level = 'hot_standby'
3
10
11
12
13
14
15
16
17
18
19
20
21
22
max_wal_senders = 10
23
wal_keep_segments = 100
24
hot_standby = on
25
archive_mode = on
26
archive_command = '/bin/true'
27
max_replication_slots = 5
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Ensure the repmgr user has appropriate permissions in pg_hba.conf and can connect in replication mode.
POSTGRES_VERSION=9.5
sudo -u postgres vim /etc/postgresql/${POSTGRES_VERSION}/main/pg_hba.conf
1
local replication repmgr trust
2
host replication repmgr 127.0.0.1/32 trust
3
host replication repmgr 192.168.1.0/24 trust
4
local repmgr repmgr trust
5
host repmgr repmgr 127.0.0.1/32 trust
6
host repmgr repmgr 192.168.1.0/24 trust
7
Adjust according to your network environment and authentication requirements. Restart PostgreSQL server.
sudo service postgresql restart
7
cluster = 'test'
8
node = 1
9 node_name = 'node1'
11 use_replication_slots = 1
12
13
14
15
16
17
18
repmgr will create a schema named after the cluster and prefixed with repmgr_, e.g. repmgr_test; we also recommend that you set the repmgr user’s
search path to include this schema name, e.g.
ALTER USER repmgr SET search_path TO repmgr_test, "$user", public;
Each server in the replication cluster will have its own record and will be updated when its status or role changes.
The configuration is slightly same as the master node. Add the following configuration into repmgr.conf:
1 cluster = 'test'
2 node = 2
3 node_name = 'node2'
5 use_replication_slots = 1
Adjust repmgr_node1 into master’s IP address/domain name in the last command. If we run the last command, we will see log output something like:
[2016-04-28 10:28:51] [NOTICE] destination directory '/var/lib/postgresql/9.5/main' provided
[2016-04-28 10:28:51] [NOTICE] starting backup (using pg_basebackup)...
[2016-04-28 10:28:51] [HINT] this may take some time; consider using the -c/--fast-checkpoint option
NOTICE: pg_stop_backup complete, all required WAL segments have been archived
[2016-04-28 10:28:53] [NOTICE] copying configuration files from master
receiving incremental file list
postgresql.conf
23,275 100% 22.20MB/s 0:00:00 (xfr#1, to-chk=0/1)
receiving incremental file list
pg_hba.conf
5,027 100% 4.79MB/s 0:00:00 (xfr#1, to-chk=0/1)
receiving incremental file list
pg_ident.conf
1,636 100% 1.56MB/s 0:00:00 (xfr#1, to-chk=0/1)
[2016-04-28 10:28:55] [NOTICE] standby clone (using pg_basebackup) complete
[2016-04-28 10:28:55] [NOTICE] you can now start your PostgreSQL server
[2016-04-28 10:28:55] [HINT] for example : pg_ctl -D /var/lib/postgresql/9.5/main start
[2016-04-28 10:28:55] [HINT] After starting the server, you need to register this standby with "repmgr standby register"
This will clone the PostgreSQL data directory files from the master at repmgr_node1 using PostgreSQL’s pg_basebackup utility. A recovery.conf file
containing the correct parameters to start streaming from this master server will be created automatically, and unless otherwise the postgresql.conf
and pg_hba.conf files will be copied from the master.
Connect to the slave node’s repmgr database and check the repl_nodes table:
repmgr=# SELECT * FROM repmgr_test.repl_nodes ORDER BY id;
id | type | upstream_node_id | cluster | name | conninfo | slot_name | priority | active
----+---------+------------------+---------+-------+---------------------------------------------+---------------+----------+--------
1 | master | | test | node1 | host=repmgr_node1 user=repmgr dbname=repmgr | repmgr_slot_1 | 100 | t
2 | standby | 1 | test | node2 | host=repmgr_node2 user=repmgr dbname=repmgr | repmgr_slot_2 | 100 | t
(2 rows)
The standby server now has a copy of the records for all servers in the replication cluster. Note that the relationship between master and standby is
explicitly defined via the upstream_node_id value, which shows here that the standby’s upstream server is the replication cluster master.
Now we already have a master-slave replication. Read more about repmgr in the Github repository (https://github.com/2ndQuadrant/repmgr).
Things you can do using repmgr:
• Cascading replication: enables a standby server to replicate from another standby server rather than directly from the master.
• Promoting a standby server: makes a standby server as a master server.
• Following a new master server: makes a standby server to replicate another master server.
• Automatic failover: detects a broken node and automatically makes a standby server to master and the others follow the new master.
• etc.
In the next article series, I will tell you how to set up a scheduled binary backup using wal-e.