1.主库关闭
[pgsql@pg1:/home/pgsql]$pg_ctl stop
waiting for server to shut down........ done
server stopped
2.检查集群状态
[pgsql@pg2:/home/pgsql]$repmgr -f /postgresql/app/postgresql/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+------+---------+---------------+----------+----------+----------+----------+-------------------------------------------------------------------------
1 | pg1 | primary | ? unreachable | ? | default | 100 | | host=192.168.1.10 port=5432 user=repmgr dbname=repmgr connect_timeout=2
2 | pg2 | standby | running | ? pg1 | default | 100 | 2 | host=192.168.1.11 port=5432 user=repmgr dbname=repmgr connect_timeout=2
3 | pg3 | standby | running | ? pg1 | default | 100 | 3 | host=192.168.1.12 port=5432 user=repmgr dbname=repmgr connect_timeout=2
4 | pg4 | witness | * running | pg3 | default | 0 | n/a | host=192.168.1.13 port=5432 user=repmgr dbname=repmgr connect_timeout=2
WARNING: following issues were detected
- unable to connect to node "pg1" (ID: 1)
- node "pg1" (ID: 1) is registered as an active primary but is unreachable
- unable to connect to node "pg2" (ID: 2)'s upstream node "pg1" (ID: 1)
- unable to determine if node "pg2" (ID: 2) is attached to its upstream node "pg1" (ID: 1)
- unable to connect to node "pg3" (ID: 3)'s upstream node "pg1" (ID: 1)
- unable to determine if node "pg3" (ID: 3) is attached to its upstream node "pg1" (ID: 1)
HINT: execute with --verbose option to see connection error messages
3.切换pg2位主节点
(1)切换Pg2为新的主
[pgsql@pg2:/home/pgsql]$repmgr -f /postgresql/app/postgresql/repmgr.conf --siblings-follow standby promote
NOTICE: promoting standby to primary
DETAIL: promoting server "pg2" (ID: 2) using "/postgresql/app/postgresql/bin/pg_ctl -w -D /postgresql/data promote"
waiting for server to promote.... done
server promoted
NOTICE: waiting up to 60 seconds (parameter "promote_check_timeout") for promotion to complete
NOTICE: STANDBY PROMOTE successful
DETAIL: server "pg2" (ID: 2) was successfully promoted to primary
NOTICE: executing STANDBY FOLLOW on 1 of 1 siblings
INFO: STANDBY FOLLOW successfully executed on all reachable sibling nodes
(2)检查
[pgsql@pg2:/home/pgsql]$repmgr -f /postgresql/app/postgresql/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+------+---------+-----------+----------+----------+----------+----------+-------------------------------------------------------------------------
1 | pg1 | primary | - failed | ? | default | 100 | | host=192.168.1.10 port=5432 user=repmgr dbname=repmgr connect_timeout=2
2 | pg2 | primary | * running | | default | 100 | 5 | host=192.168.1.11 port=5432 user=repmgr dbname=repmgr connect_timeout=2
3 | pg3 | standby | running | pg2 | default | 100 | 3 | host=192.168.1.12 port=5432 user=repmgr dbname=repmgr connect_timeout=2
4 | pg4 | witness | * running | pg3 | default | 0 | n/a | host=192.168.1.13 port=5432 user=repmgr dbname=repmgr connect_timeout=2
WARNING: following issues were detected
- unable to connect to node "pg1" (ID: 1)
HINT: execute with --verbose option to see connection error messages
4.写入数据测试
(1)写入数据
sspudb=> insert into my_test3 values(4,'xsq4'),(5,'xsq5');
INSERT 0 2
sspudb=> select * from my_test3;
id | name
----+------
1 | xsq2
2 | xsq2
3 | xsq3
4 | xsq4
5 | xsq5
(5 rows)
(2)从库数据检查
[pgsql@pg3:/home/pgsql]$psql -U sspu -d sspudb
Password for user sspu:
psql (15.3)
Type "help" for help.
sspudb=> select * from my_test3;
id | name
----+------
1 | xsq2
2 | xsq2
3 | xsq3
4 | xsq4
5 | xsq5
(5 rows)