> 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.md).

# Deploy Cennso PostgreSQL

This guide describes how to deploy the Cennso PostgreSQL Operator and a Cennso PostgreSQL Instance to get a ready-to-use PostgreSQL cluster of two instances with [async replication](https://www.postgresql.org/docs/current/runtime-config-replication.html).

The deployment consists of two stages, described in the next two sections:

1. [Deploy Cennso PostgreSQL Operator](#deploy-cennso-postgresql-operator)
2. [Deploy Cennso PostgreSQL Instance](#deploy-cennso-postgresql-instance)

After the deployment, you can [validate your PostgreSQL cluster](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-postgresql-validation.md), [create database users](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-users-creation.md), and start to use your database cluster.

> [!TIP]
> If any issues occur during component deployment, see the [troubleshooting](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/troubleshooting.md) guide.

The following examples deploy both the operator and the instance directly to the **Management Cluster**. To deploy to a remote Core Cluster instead, set **spec.connectionSecretRef** on both CRs. For more information, see the [Kubernetes cluster secret reference](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-operator.md#kubernetes-cluster-secret-reference) section.

## Deploy Cennso PostgreSQL Operator

This section shows how to deploy the Cennso PostgreSQL Operator directly to the Management Cluster. After the deployment, the operator Pod runs in the `{OPERATOR-NAMESPACE}` Kubernetes Namespace and manages the PostgreSQL clusters that `PostgreSQLInstance` resources create in the `{NAMESPACE-WITH-DATABASE-INSTANCES}` Kubernetes Namespace.

To deploy the Cennso PostgreSQL Operator, execute the following steps:

1. Prepare the `PostgreSQLOperator` Custom Resource (CR) to deploy the Cennso PostgreSQL Operator in your Management Cluster. For example:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: PostgreSQLOperator
   metadata:
     name: {POSTGRESQL-OPERATOR-NAME}
     namespace: cennso-infra
   spec:
     operatorNamespace: {OPERATOR-NAMESPACE}
     watchNamespaces:
       - {NAMESPACE-WITH-DATABASE-INSTANCES}
     storage:
       classes:
         - name: postgresql-data-storage
           nodePath: /opt/local-path-provisioner/postgresql
   ```

   > [!IMPORTANT]
   > The **spec.storage.classes[].nodePath** must be one of the paths configured in the local-path-provisioner of the target cluster. For more information, see the [Storage configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-operator.md#storage-configuration) section.

   For more information about available configuration options, see the [Cennso PostgreSQL Operator](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-operator.md) and [Cennso PostgreSQL Operator Custom Resource Definition](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-postgresql/xpostgresqloperator_definition.md) documents.

2. Apply the prepared CR to your Management Cluster:

   ```bash
   kubectl apply -f postgresql-operator-cr.yaml
   ```

3. Check your target cluster for the ready Cennso PostgreSQL Operator Pod:

   ```bash
   kubectl get pods -n {OPERATOR-NAMESPACE}
   ```

   Check the state of the `READY` column in the output. As soon as all the containers within the Cennso PostgreSQL Operator Pod are ready, you can move to the deployment of the PostgreSQL cluster.

## Configure TLS certificates for the PostgreSQL cluster

> [!NOTE]
> This is an optional step. If the default TLS certificates are sufficient for your PostgreSQL cluster, move directly to the [Deploy Cennso PostgreSQL Instance](#deploy-cennso-postgresql-instance) section.

By default, each PostgreSQL cluster uses TLS certificates requested by the Cennso PostgreSQL Operator from the [cert-manager](https://cert-manager.io/). These certificates are valid for one year, and cert-manager renews them `30` days before they expire. To customize the validity time of the TLS certificates for your PostgreSQL cluster, set the following configuration options in the `PostgreSQLInstance` Custom Resource:

```yaml
spec:
  tls:
    certValidityDuration: 2160h # 90 days
    caValidityDuration: 17520h # 2 years
```

Both values must be longer than `720h`. 

For more information, see the [TLS certificates configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#tls-certificates-configuration) section.

If you want to use your own TLS certificates instead, you can find more information in the [Manage TLS certificates](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-certificates.md) document.

## Deploy Cennso PostgreSQL Instance

After you have deployed the Cennso PostgreSQL Operator, you can deploy a PostgreSQL cluster using the `PostgreSQLInstance` CR.

This section shows an example of how to deploy a two-instance PostgreSQL cluster directly to the Management Cluster, in the same `{NAMESPACE-WITH-DATABASE-INSTANCES}` Kubernetes Namespace that the operator from the [Deploy Cennso PostgreSQL Operator](#deploy-cennso-postgresql-operator) section watches.

> [!NOTE]
> The deployment described in this example does not contain information about the PostgreSQL exporter which collects metrics from the PostgreSQL instance(s).
>
> To learn how to enable and configure observability for the PostgreSQL cluster, read the [Enable metrics collector](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-postgresql-observability.md) document.

To deploy a Cennso PostgreSQL Instance, execute the following steps:

1. Ensure you have the Cennso PostgreSQL Operator deployed in your cluster, and the Kubernetes Namespace you want to use for the PostgreSQL cluster deployment is mentioned in the **spec.watchNamespaces** field of the Cennso PostgreSQL Operator CR.

2. Ensure the `StorageClass` you reference in **spec.instances[].storage.className** exists on the target cluster. You can do this using the following command:

   ```bash
   kubectl get storageclass postgresql-data-storage
   ```

   The output should be similar to the following:

   ```
   NAME                      PROVISIONER                            RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
   postgresql-data-storage   cluster.local/local-path-provisioner   Retain          WaitForFirstConsumer   false                  5m
   ```

   The example below uses `postgresql-data-storage`, which the operator from the [Deploy Cennso PostgreSQL Operator](#deploy-cennso-postgresql-operator) section creates through its **spec.storage.classes**. Any other StorageClass capable of dynamically provisioning PersistentVolumeClaims, as for example, [`openebs-hostpath`](https://openebs.io/docs/3.3.x/user-guides/localpv-hostpath) should work as well. `PostgreSQLInstance` does not require node-local paths to exist in advance if dynamic provisioning is sufficient. If you prefer node-local volumes on specific nodes, see the [Scheduling and placement configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#scheduling-and-placement-configuration) section instead. In that case, the directories must exist on the nodes beforehand.

3. Prepare the `PostgreSQLInstance` Custom Resource (CR). This CR defines the deployment target, the instance sets, and their storage. For example:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: PostgreSQLInstance
   metadata:
     name: {POSTGRESQL-INSTANCE-NAME}
     namespace: cennso-infra
   spec:
     namespace: {NAMESPACE-WITH-DATABASE-INSTANCES}
     instances:
       - name: instance1
         replicas: 2
         storage:
           capacity: 20Gi
           className: postgresql-data-storage
     proxy:
       pgBouncer:
         replicas: 2
     metricsExporter:
       enabled: false
   ```

   > [!IMPORTANT]
   > The **spec.namespace** (`{NAMESPACE-WITH-DATABASE-INSTANCES}`) must be listed in the operator's **spec.watchNamespaces**.
   >
   > The `{POSTGRESQL-INSTANCE-NAME}` must not exceed `38` characters. For more information, see the [Naming constraint](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#naming-constraint) section.

   For more information about available configuration options, including PostgreSQL major-version upgrades, see the [Cennso PostgreSQL Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md) and [Cennso PostgreSQL Instance Custom Resource Definition](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-postgresql/xpostgresqlinstance_definition.md) documents.

4. Apply the prepared CR to your Management Cluster:

   ```bash
   kubectl apply -f postgresql-instance-cr.yaml
   ```

5. Check for the ready PostgreSQL cluster pods:

   ```bash
   kubectl get pods -n {NAMESPACE-WITH-DATABASE-INSTANCES}
   ```

   As soon as all the containers within the PostgreSQL and [PgBouncer](https://www.pgbouncer.org/) pods are ready, you can start using the database.
