> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-change-roles.md).

# 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:

```bash
kubectl exec -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database -- patronictl switchover --primary {CURRENT-PRIMARY-MEMBER} --candidate {REPLICA-MEMBER} --force
```

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](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/patroni.md) 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
  kubectl get pods -n {NAMESPACE} -l postgres-operator.crunchydata.com/role=primary -o jsonpath='{.items[0].metadata.name}{"\n"}'
  ```

- `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:

  ```bash
  kubectl get pods -n {NAMESPACE} -l postgres-operator.crunchydata.com/role=replica -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
  ```

  Choose a replica in the `streaming` state with a lag of `0`, 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
  kubectl exec -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database -- patronictl list
  ```

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 [PgBouncer](https://www.pgbouncer.org/) 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](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/patroni.md) document.
