Postgresql Master/Slave Using Patroni
The whole point of Patroni is that you don't have to do that yourself, but that Patroni does it for you automatically.

The nodes communicate regularly via a distributed consensus store that decides who is online and who isn't, and arbiters who of the available nodes should become the leader.

You can also tell Patroni manually to switch leader, even if all nodes are up (a "switchover").