> 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/deployment-guides/cennso-postgresql-validation.md).

# Validate the PostgreSQL cluster

After you applied the Cennso PostgreSQL CRs to the target cluster, the PostgreSQL cluster is deployed. It consists of the multiple instances specified by **spec.instances** of the `PostgreSQLInstance` CR.

One of the PostgreSQL instances is the primary instance that accepts both reads and writes. The others are read replicas only. You can differentiate them by the `postgres-operator.crunchydata.com/role` label. This label has the value `primary` for the PostgreSQL instance selected as the primary instance and `replica` for the read replicas.

This document describes the validation procedures after your PostgreSQL cluster is up and running.

## Connect to the PostgreSQL cluster

Applications must use [PgBouncer](https://www.pgbouncer.org/) instead of connecting directly to a pod or a service. The Cennso Database extension exposes two kinds of PgBouncer endpoints:

- [In-cluster endpoint](#in-cluster-endpoint)
- [External endpoint](#external-endpoint)

### In-cluster endpoint

The Cennso PostgreSQL Instance creates a Kubernetes Service that routes traffic to all PgBouncer pods. Applications running in the same cluster must use this Service as the PostgreSQL endpoint. The endpoint is reachable through the following endpoints:

- `{POSTGRESQL-INSTANCE-NAME}-pgbouncer:5432`
- `{POSTGRESQL-INSTANCE-NAME}-pgbouncer.{POSTGRESQL-CLUSTER-NAMESPACE}.svc.cluster.local:5432`

The `{POSTGRESQL-INSTANCE-NAME}` is the value specified in the **metadata.name** of the `PostgreSQLInstance` CR, and the `{POSTGRESQL-CLUSTER-NAMESPACE}` is the value of its **spec.namespace**.

For example, to connect from a Pod in the same cluster as the default database user, run:

```bash
psql "host={POSTGRESQL-INSTANCE-NAME}-pgbouncer.{POSTGRESQL-CLUSTER-NAMESPACE}.svc.cluster.local port=5432 user={POSTGRESQL-INSTANCE-NAME} dbname={POSTGRESQL-INSTANCE-NAME} sslmode=require"
```

Connections to PgBouncer are always encrypted. A connection with `sslmode=disable` is rejected.

The password for the default database user is stored in the `{POSTGRESQL-INSTANCE-NAME}-pguser-{POSTGRESQL-INSTANCE-NAME}` Kubernetes Secret in the same namespace as the PostgreSQL cluster. You can retrieve it with:

```bash
kubectl get secret {POSTGRESQL-INSTANCE-NAME}-pguser-{POSTGRESQL-INSTANCE-NAME} \
  -n {POSTGRESQL-CLUSTER-NAMESPACE}                                             \
  --template='{{.data.password | base64decode}}{{"\n"}}'
```

The same Secret also holds a ready-to-use connection string for PgBouncer in the `pgbouncer-uri` key.

### External endpoint

By default, the PgBouncer Service is of the `ClusterIP` type and is reachable only from inside the cluster. To reach the PostgreSQL cluster from the outside, set **spec.proxy.pgBouncer.expose.type** of the `PostgreSQLInstance` CR to `NodePort` or `LoadBalancer`. For more information, see the [PgBouncer configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#pgbouncer-configuration) section of the Cennso PostgreSQL Instance document.

## Validate replication state

Once the primary and the read replicas are ready, you can check the replication status:

1. List the PostgreSQL pods in the cluster together with their roles:

   ```bash
   kubectl get pods -n {POSTGRESQL-CLUSTER-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 exactly one pod and `replica` for the others. The `postgres-operator.crunchydata.com/instance` label limits the list to the PostgreSQL pods.

2. Check the state of the cluster with [Patroni](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/patroni.md), which runs inside every PostgreSQL pod. Run the following command in the `database` container of any PostgreSQL pod:

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

   The output lists every member of the cluster. For example:

   ```
   + Cluster: {POSTGRESQL-INSTANCE-NAME}-ha (7693527883571142716) ---------------------------------------------------------------+---------+-----------+----+-------------+-----+------------+-----+
   |                    Member                   |                                    Host                                       |   Role  |   State   | TL | Receive LSN | Lag | Replay LSN | Lag |
   +---------------------------------------------+-------------------------------------------------------------------------------+---------+-----------+----+-------------+-----+------------+-----+
   | {POSTGRESQL-INSTANCE-NAME}-instance1-abcd-0 | {POSTGRESQL-INSTANCE-NAME}-instance1-abcd-0.{POSTGRESQL-INSTANCE-NAME}-pods   | Leader  | running   |  1 |             |     |            |     |
   | {POSTGRESQL-INSTANCE-NAME}-instance1-efgh-0 | {POSTGRESQL-INSTANCE-NAME}-instance1-efgh-0.{POSTGRESQL-INSTANCE-NAME}-pods   | Replica | streaming |  1 |   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 the primary in receiving WAL. The second column shows how far it is behind in replaying WAL. All members share the same timeline in the `TL` column.

3. Execute the following SQL query on the primary pod to see the replication connections from the PostgreSQL side:

   ```bash
   kubectl exec -n {POSTGRESQL-CLUSTER-NAMESPACE} {POSTGRESQL-PRIMARY-POD-NAME} -c database -- psql -c "SELECT client_addr, state, sync_state, replay_lag FROM pg_stat_replication;"
   ```

   The output contains one row per read replica. For example:

   ```
    client_addr | state     | sync_state | replay_lag
   -------------+-----------+------------+-----------------
    10.42.0.11  | streaming | async      | 00:00:00.001234
   ```

   The `state` column shows `streaming` for every read replica. This means that you have a fully working and ready-to-use PostgreSQL cluster.

If a member of the cluster is unhealthy, see the [Troubleshoot using Patroni](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/patroni.md) document.
