os: ubunbu 16.04
postgresql: 9.6.8
repmgr: 4.1.1
192.168.56.101 node1
192.168.56.102 node2
Install postgresql 9.6 software
Both node1 and node2 need to use apt install to install postgresql9.6.8. The last bit represents the patch number
# apt install postgresql-plpython-9.6 postgresql-plperl-9.6 postgresql-pltcl-9.6 postgresql-doc-9.6 postgresql-contrib-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-9.6 postgresql-server-dev-9.6 libpq-dev libpq5
The advantage of using apt or yum is that the dependency package will be installed, which is more convenient.
After installation, initdb will automatically initialize the database cluster. The main thing is that the files under PG ﹣ data on node2 need to be deleted, because the data needs to be synchronized from node1.
More detailed process.
Set PG? Bindir
Both node1 and node2 need to be set
# vi /etc/profile PG_BINDIR=/usr/lib/postgresql/9.6/bin
Install repmgr
Both node1 and node2 need to install 2ndQuadrant public apt repository for Debian/Ubuntu
# curl https://dl.2ndquadrant.com/default/release/get/deb | sudo bash # apt install postgresql-9.6-repmgr repmgr-common
Create user and database
Operation on node1
$ createuser -s repmgr $ createdb repmgr -O repmgr $ psql -c "alter user repmgr with password 'repmgrrepmgr'" $ psql -c "alter user repmgr set search_path to repmgr, \"\$user\", public";
Modify pg_hba.conf
node1 and node2 need to be operated. In fact, the configuration file of each node in the stream replication environment should be the same, which is convenient for switching
$ vi /etc/postgresql/9.6/main/pg_hba.conf local repmgr repmgr trust host repmgr repmgr 127.0.0.1/32 trust host repmgr repmgr 192.168.56.0/24 trust local replication repmgr trust host replication repmgr 127.0.0.1/32 trust host replication repmgr 192.168.56.0/24 trust
Modify postgresql.conf
node1 and node2 need to be operated. In fact, the configuration file of each node in the stream replication environment should be the same, which is convenient for switching
It needs to be set to the parameter recommended by stream replicator. One of the parameters needs to be modified so as not to restart the instance after modification.
$ vi /etc/postgresql/9.6/main/postgresql.conf shared_preload_libraries = 'repmgr,pg_stat_statements'
/etc/repmgr.conf
The file contents of node1 node are as follows:
$ vi /etc/repmgr.conf node_id=1 node_name=node1 conninfo='host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2' data_directory='/var/lib/postgresql/9.6/main'
The file contents of node2 node are as follows:
$ vi /etc/repmgr.conf node_id=2 node_name=node2 conninfo='host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2' data_directory='/var/lib/postgresql/9.6/main'
Register the primary server
Operation on node1 node
$ repmgr -f /etc/repmgr.conf primary register $ repmgr -f /etc/repmgr.conf cluster show $ psql -U repmgr repmgr=# SELECT * FROM repmgr.nodes;
Clone the standby server
Operation on node2 node
$ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run $ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
– dry run means
--dry-run show what would happen for action, but don't execute it
Verify replication is functioning
View on node1 node
$ psql -U repmgr repmgr=# SELECT * FROM pg_stat_replication;
View on node2 node
$ psql -U repmgr repmgr=# select * from pg_stat_wal_receiver;
Register the standby
$ repmgr -f /etc/repmgr.conf standby register $ repmgr -f /etc/repmgr.conf cluster show ID | Name | Role | Status | Upstream | Location | Connection string ----+-------+---------+-----------+----------+----------+----------------------------------------------------------------- 1 | node1 | primary | * running | | default | host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2 2 | node2 | standby | running | node1 | default | host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2
You can see that the result of cluster show meets the expectation.
The next blog introduces the switchover of manual repmgr
Reference resources:
https://repmgr.org/docs/4.1/index.html