> 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/troubleshooting/patroni.md).

# Troubleshoot using Patroni

To be sure that the PostgreSQL cluster is up and running and replication is in sync, you can use [Patroni](https://patroni.readthedocs.io/), which runs inside every PostgreSQL instance deployed by the Cennso PostgreSQL Instance. Patroni has no user interface. You inspect the cluster with the `patronictl` utility inside a PostgreSQL pod.

To check the state of the PostgreSQL cluster, execute the following steps:

1. List the PostgreSQL pods in the cluster. The operator labels every pod with its current role, so you can see the primary and the replicas:

   ```bash
   kubectl get pods -n {NAMESPACE} -l postgres-operator.crunchydata.com/cluster={POSTGRESQL-INSTANCE-NAME},postgres-operator.crunchydata.com/instance -L postgres-operator.crunchydata.com/role
   ```

   The `ROLE` column shows `primary` for the primary and `replica` for the replicas. The `postgres-operator.crunchydata.com/instance` label limits the list to the PostgreSQL pods.

2. Run `patronictl list` inside the `database` container of any PostgreSQL pod:

   ```bash
   kubectl exec -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database -- patronictl list
   ```

   The output lists every member of the cluster with its role, state, timeline, and replication lag. For example, for a `PostgreSQLInstance` named `pg-instance`:

   ```
   + Cluster: pg-instance-ha (7693527883571142716) -------------------------------+---------+-----------+----+-------------+-----+------------+-----+
   |            Member            |                      Host                     |   Role  |   State   | TL | Receive LSN | Lag | Replay LSN | Lag |
   +------------------------------+-----------------------------------------------+---------+-----------+----+-------------+-----+------------+-----+
   | pg-instance-instance1-abcd-0 | pg-instance-instance1-abcd-0.pg-instance-pods | Leader  | running   |  3 |             |     |            |     |
   | pg-instance-instance1-efgh-0 | pg-instance-instance1-efgh-0.pg-instance-pods | Replica | streaming |  3 |   0/E000000 |   0 |  0/E000000 |   0 |
   +------------------------------+-----------------------------------------------+---------+-----------+----+-------------+-----+------------+-----+
   ```

   A healthy cluster shows exactly one `Leader` in the `running` state and every replica in the `streaming` state with both `Lag` columns close to `0`. The first `Lag` column shows how far the replica is behind in receiving the WAL from the primary, and the second one shows how far it is behind in replaying it. All members share the same timeline in the `TL` column. A replica in the `stopped` or `start failed` state, a growing lag, or a member on an older timeline points to a replication problem.

3. If a member is unhealthy, read the Patroni and PostgreSQL logs of its pod:

   ```bash
   kubectl logs -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database
   ```

## Switch the primary to another replica

Patroni promotes a replica automatically when the primary becomes unavailable, so no manual action is required. A manual switchover is useful when the primary is healthy, but you want to move it, for example before you drain the node that runs it for maintenance.

A switchover is a controlled role change. Patroni makes the chosen replica the new primary, and the old primary joins the cluster again as a replica. Client connections through [PgBouncer](https://www.pgbouncer.org/) are interrupted for a few seconds while the roles change.

To switch the primary to another replica, execute the following steps:

1. List the members of the cluster and note the names of the current primary and of the replica you want to promote:

   ```bash
   kubectl exec -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database -- patronictl list
   ```

   Choose a replica in the `streaming` state with a lag of `0`, so that no committed transactions are lost during the switchover.

2. Run the switchover from any PostgreSQL pod. The `--force` option skips the interactive confirmation:

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

   Replace the following placeholders with the member names from step 1:

   - `{CURRENT-PRIMARY-MEMBER}` - the member shown as `Leader`
   - `{REPLICA-MEMBER}` - the member you want to promote

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

3. Check the result. The chosen replica is now the `Leader`, the former primary is a `Replica` in the `streaming` state, and the timeline in the `TL` column increased by one:

   ```bash
   kubectl exec -n {NAMESPACE} {POSTGRESQL-POD-NAME} -c database -- patronictl list
   ```

The operator updates the role labels of the pods and the primary Service after the switchover, so clients keep connecting through the same endpoints.
