There are many different solutions; but I would recommend and use a least a three node cluster using synchronous
replicationwhere one of the nodes is acting as the witness — at a minimum (actual have more replicas). The witness
nodeneed not be a full Postgres instance; it can also be achieved by using pg_recievexlog as the witness instead. This
setupavoids a the split brain situation by making sure there is only one writable instance and that is the one that the
witnessis following.
I use CARP (Common Address Redundancy Protocol) to manage the HA IP address (very platform specific); when the address
flipsover to a replica it automatically promotes the replica and witness follows the HA address thus signing off on the
selfpromotion.
The old primary then needs to be reconfigured as a replia which can be done using pg_rewind or other numerous other
solutions;i.e. snapshots, backup, etc.
I have not personally used this solution; but you could look at 2ndQuadrant repmgr product if you’re looking a packaged
solution.
https://www.2ndquadrant.com/en/resources/repmgr/installation-instructions/