Postgresql Patroni
Postgresql Patroni
This document offers a comprehensive guide on configuring Patroni for a three-node PostgreSQL setup. The following steps need to be
executed.
Setup ETCD
1. Configure PostgreSQL on all three machines without initializing the database.
2. Setup ETCD on all nodes and follow the steps outlined below.
wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz
Create an etcd user and group specifically for running etcd binaries.
Create an etcd user and group specifically for running etcd binaries.
Check the status of the endpoints to ensure they are functioning properly.
Set up Patroni
Set up Patroni on all three nodes and proceed with the following steps.
Node1 patroni.yml
1 scope: bootvar
2 name: pgdb1
3
4 log:
5 traceback_level: INFO
6 level: INFO
7 dir: /etc/patroni/logs/
8 file_num: 5
9
10 restapi:
11 listen: 0.0.0.0:8008
12 connect_address: 172.20.0.2:8008
13
14 etcd3:
15 protocol: http
16 hosts: 172.20.0.2:2379,172.20.0.3:2379,172.20.0.4:2379
17
18 bootstrap:
19 dcs:
20 ttl: 30
21 loop_wait: 10
22 retry_timeout : 10
23 maximum_lag_on_failover: 1048576
24 postgresql:
25 use_pg_rewind: true
26 use_slots: true
27 parameters:
28 wal_keep_segments: 100
29 #add other postgres DB parameters to start with
30
31 initdb:
32 - encoding: UTF8
33 - data-checksums
34
35 pg_hba:
36 - host replication replicator 0.0.0.0/0 md5
37 - host all all 0.0.0.0/0 md5
38
39 postgresql:
40 listen: 172.20.0.2:5432
41 connect_address: 172.20.0.2:5432
42 data_dir: /var/lib/pgsql/bootvar/pgdb1
43 bin_dir: /usr/pgsql-16/bin
44 authentication:
45 replication:
46 username: replicator
47 password: test
48 superuser:
49 username: postgres
50 password: test
Node2 patroni.yml
1 scope: bootvar
2 name: pgdb2
3
4 log:
5 traceback_level: INFO
6 level: INFO
7 dir: /etc/patroni/logs/
8 file_num: 5
9
10 restapi:
11 listen: 0.0.0.0:8008
12 connect_address: 172.20.0.3:8008
13
14 etcd3:
15 protocol: http
16 hosts: 172.20.0.2:2379,172.20.0.3:2379,172.20.0.4:2379
17
18 bootstrap:
19 dcs:
20 ttl: 30
21 loop_wait: 10
22 retry_timeout : 10
23 maximum_lag_on_failover: 1048576
24 postgresql:
25 use_pg_rewind: true
26 use_slots: true
27 parameters:
28 wal_keep_segments: 100
29 #add other postgres DB parameters to start with
30
31 initdb:
32 - encoding: UTF8
33 - data-checksums
34
35 pg_hba:
36 - host replication replicator 0.0.0.0/0 md5
37 - host all all 0.0.0.0/0 md5
38
39 postgresql:
40 listen: 172.20.0.3:5432
41 connect_address: 172.20.0.3:5432
42 data_dir: /var/lib/pgsql/bootvar/pgdb2
43 bin_dir: /usr/pgsql-16/bin
44 authentication:
45 replication:
46 username: replicator
47 password: test
48 superuser:
49 username: postgres
50 password: test
51
Node3 patroni.yml
1 scope: bootvar
2 name: pgdb3
3
4 log:
5 traceback_level: INFO
6 level: INFO
7 dir: /etc/patroni/logs/
8 file_num: 5
9
10 restapi:
11 listen: 0.0.0.0:8008
12 connect_address: 172.20.0.4:8008
13
14 etcd3:
15 protocol: http
16 hosts: 172.20.0.2:2379,172.20.0.3:2379,172.20.0.4:2379
17
18 bootstrap:
19 dcs:
20 ttl: 30
21 loop_wait: 10
22 retry_timeout : 10
23 maximum_lag_on_failover: 1048576
24 postgresql:
25 use_pg_rewind: true
26 use_slots: true
27 parameters:
28 wal_keep_segments: 100
29 #add other postgres DB parameters to start with
30
31 initdb:
32 - encoding: UTF8
33 - data-checksums
34
35 pg_hba:
36 - host replication replicator 0.0.0.0/0 md5
37 - host all all 0.0.0.0/0 md5
38
39 postgresql:
40 listen: 172.20.0.4:5432
41 connect_address: 172.20.0.4:5432
42 data_dir: /var/lib/pgsql/bootvar/pgdb3
43 bin_dir: /usr/pgsql-16/bin
44 authentication:
45 replication:
46 username: replicator
47 password: test
48 username: postgres
49 password: test
Create a table in the primary database and ensure its replication in the standby database is successfully established.
Node1:
Node2:
Node3:
After the switchover, a new row should be inserted on node2 for the newly promoted leader.
Node2:
Node1:
Node3: