Gå til indholdet

Switching the LEADER node

In some maintenance cases, it may be useful to switch the PostgreSQL cluster LEADER node in order to perform operations on it.

Before you can change the LEADER node, please identify the current LEADER node of the PostgreSQL cluster and then connect to it as root.
Once connected as root to the LEADER node, run a command similar to the following to trigger an immediate switchover:

Value Variable Definition
PSQL_NODE Name of the node that will become LEADER.
1
patronictl -c /etc/patroni/config.yml failover --candidate PSQL_NODE --force

Failure

If the command fails, check that the settings for patronictl have been applied on each node: the Patroni port 8008 must be open in the local firewall, and patronictl must trust the certificate of the Patroni API (otherwise the command displays certificate verify failed).

Example

For the following PostgreSQL cluster:

1
2
3
4
5
6
7
+ Cluster: 17-cleanroomvault5 ------+---------+-----------+----+-----------+
| Member           | Host           | Role    | State     | TL | Lag in MB |
+------------------+----------------+---------+-----------+----+-----------+
| PSQL_3           | psql_3         | Leader  | running   | 2  |           |
| + PSQL_1         | psql_1         | Replica | streaming | 2  | 0         |
| + PSQL_2         | psql_2         | Replica | streaming | 2  | 0         |
+------------------+----------------+---------+-----------+----+-----------+

The goal is to make the PSQL_1 node the LEADER.
You therefore first connect as root to the PSQL_3 node, which is the current LEADER.
Once connected, the following command immediately switches the LEADER to the PSQL_1 node:

1
patronictl -c /etc/patroni/config.yml failover --candidate PSQL_1 --force

The change of topology is then visible and can be checked:

1
2
3
4
5
6
7
+ Cluster: 17-cleanroomvault5 ------+---------+-----------+----+-----------+
| Member           | Host           | Role    | State     | TL | Lag in MB |
+------------------+----------------+---------+-----------+----+-----------+
| PSQL_1           | psql_1         | Leader  | running   | 3  |           |
| + PSQL_3         | psql_3         | Replica | streaming | 3  | 0         |
| + PSQL_2         | psql_2         | Replica | streaming | 3  | 0         |
+------------------+----------------+---------+-----------+----+-----------+