Change database roles
Sometimes it might be necessary to change the roles of the existing database instances, for example, to perform maintenance on the database node. In this case, you need to assign the primary role to another database instance.
To do this, run the following command in the database container of any PostgreSQL pod:
Replace each placeholder with an appropriate value:
-
NAMESPACE- the Kubernetes namespace where the PostgreSQL instance is running. -
POSTGRESQL-POD-NAME- any PostgreSQL server pod name of the cluster. -
CURRENT-PRIMARY-MEMBER- the Patroni member name of the current primary. The member name is the name of the primary PostgreSQL pod. You can find it using the following command:bash -
REPLICA-MEMBER- the Patroni member name of the replica you want to promote. The member name is the name of the replica PostgreSQL pod. You can list the replicas using the following command:bashChoose a replica in the
streamingstate with a lag of0, so that no committed transactions are lost during the switchover. You can check the state and the replication lag of the replicas using the following command:bash
The command sets the PostgreSQL server running as REPLICA-MEMBER as the primary. The former primary joins the cluster again as a replica. The --force option skips the interactive confirmation. To run the switchover at a later time, add --scheduled {TIMESTAMP} with an ISO 8601 timestamp, for example 2026-10-05T22:00:00+00:00.
Important
While the roles switch, the client connections through PgBouncerOpens in a new tab are interrupted for a few seconds. The operator updates the role labels of the pods and the primary Service after the switchover, so the new connections are established to the new primary through the same endpoints. The affected application is responsible for reconnecting.
For more information on how to check the state of the PostgreSQL cluster, see the Troubleshoot using Patroni document.