> 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/concepts/cennso-postgresql-schema.md).

# Cennso PostgreSQL Schema

A Cennso PostgreSQL Schema is represented by the `PostgreSQLSchema` Custom Resource (CR). Applying this CR to a Management Cluster creates a Kubernetes Job that provisions a logical PostgreSQL database inside an existing [Cennso PostgreSQL Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md).

> [!IMPORTANT]
> Before provisioning a logical database, the target `PostgreSQLInstance` must be deployed, and its [PgBouncer](https://www.pgbouncer.org/) Kubernetes Service must be reachable from the provisioning Job. The Job connects to the PostgreSQL cluster through that Service and authenticates as the default database user with the credentials stored in the `{POSTGRESQL-INSTANCE-NAME}-pguser-{POSTGRESQL-INSTANCE-NAME}` Kubernetes Secret.
>
> `PostgreSQLSchema` provisions a PostgreSQL database, not a PostgreSQL `SCHEMA` object, which is a namespace inside a database.

The sections below provide information about configuration parameters that you can specify in the `PostgreSQLSchema` CR. For more information on how to provision a logical database, see the [Create a PostgreSQL database](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-database-creation.md) document.

## Kubernetes cluster secret reference

To deploy the Cennso PostgreSQL Schema to a Core Cluster, you must specify a reference to the [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) so the Core Cluster is accessible. You can do this using the following configuration parameters:

```yaml
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: {CORE-CLUSTER-SECRET-NAMESPACE}
```

Set **spec.connectionSecretRef.name** to the name of the Core Cluster that contains the PostgreSQL cluster where the database should be created. The **spec.connectionSecretRef.namespace** configuration parameter defines the Kubernetes Namespace on the Management Cluster where the Kubernetes secret to access this Core Cluster is stored.

If you want to deploy the Cennso PostgreSQL Schema to the Management Cluster, do not specify **spec.connectionSecretRef**. In both cases, the target cluster must be the same as the one of the Cennso PostgreSQL Operator that manages the namespace of the database instances.

## Target PostgreSQL Instance reference

To select the PostgreSQL cluster where the logical database is created, you must specify the `PostgreSQLInstance` name and the Kubernetes Namespace where it is deployed:

```yaml
spec:
  namespace: {POSTGRESQL-CLUSTER-NAMESPACE}
  dbInstanceName: {POSTGRESQL-INSTANCE-NAME}
```

The **spec.namespace** must match **spec.namespace** of the `PostgreSQLInstance`.

The **spec.dbInstanceName** must match the **metadata.name** of an existing `PostgreSQLInstance` resource. The schema-provisioning Job connects to the `{POSTGRESQL-INSTANCE-NAME}-pgbouncer.{POSTGRESQL-CLUSTER-NAMESPACE}.svc:5432` Kubernetes Service and uses the credentials from the `{POSTGRESQL-INSTANCE-NAME}-pguser-{POSTGRESQL-INSTANCE-NAME}` Kubernetes Secret.

## Database name

To define the logical database that should be created, use the following configuration parameter:

```yaml
spec:
  schemaName: {DATABASE-NAME}
```

The **spec.schemaName** must be a valid, unquoted PostgreSQL identifier: a letter or underscore, followed by letters, digits, or underscores, up to `63` characters.

> [!NOTE]
> `PostgreSQLSchema` only creates the logical database. It does not update or delete it. Deleting a `PostgreSQLSchema` Custom Resource does not drop the provisioned database from PostgreSQL. To delete a schema, use the [DROP SCHEMA](https://www.postgresql.org/docs/current/sql-dropschema.html) SQL statement.

## Job retries, lifetime, and deadline

To control how the schema-provisioning [Kubernetes Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/) behaves on failure and after completion, use the following configuration parameters:

```yaml
spec:
  backoffLimit: 1
  ttlSecondsAfterFinished: 3600
  activeDeadlineSeconds: 600
  restartPolicy: OnFailure
```

The **spec.backoffLimit** configures the number of retries before the Job is marked as failed. The default value is `1`. For more information, see the [pod backoff failure policy](https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-backoff-failure-policy) documentation.

The **spec.ttlSecondsAfterFinished** configures how long a finished Job (either `Complete` or `Failed`) is retained before it is automatically cleaned up. If omitted, the Job is not deleted automatically. For more information, see the [TTL mechanism for finished Jobs](https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/) documentation.

> [!NOTE]
> The Job exists as long as the `PostgreSQLSchema` exists. After **spec.ttlSecondsAfterFinished** expires, the Job is deleted and then created again. The new Job run does not change the database because it creates the database only if it does not already exist. However, each new run starts a new Pod. Leave **spec.ttlSecondsAfterFinished** unset to avoid this behavior.

The **spec.activeDeadlineSeconds** configures a wall-clock deadline, measured from the Job start time, after which the Job is terminated and marked as failed. The default value is `600` seconds.

The **spec.restartPolicy** configures the restart policy of the Job Pod. The supported values are `OnFailure` and `Never`. The default value is `OnFailure`. For more information, see the [pod lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy) documentation.

## Readiness and failed Jobs

The `PostgreSQLSchema` becomes `Ready` when the Job has completed. A `PostgreSQLSchema` that stays not ready has a Job that is still running or has failed. You can use the following commands to check the Job and its Pod in **spec.namespace** on the target cluster:

```bash
kubectl -n {POSTGRESQL-CLUSTER-NAMESPACE} get job {POSTGRESQLSCHEMA-NAME}-schema
kubectl -n {POSTGRESQL-CLUSTER-NAMESPACE} logs job/{POSTGRESQLSCHEMA-NAME}-schema
```

A failed Job is not restarted automatically. To run it again, delete the Job, and a new one will be created:

```bash
kubectl -n {POSTGRESQL-CLUSTER-NAMESPACE} delete job {POSTGRESQLSCHEMA-NAME}-schema
```

The same applies after changing **spec.image**, **spec.restartPolicy**, or **spec.schemaName** of an existing `PostgreSQLSchema`. The Pod template of a Kubernetes Job cannot be changed, so the update is rejected until the existing Job is deleted.

## Image and pull secret

You can configure the image of the schema-provisioning Job and the Kubernetes Secret used to pull it with the following configuration options:

```yaml
spec:
  image:
    registry: registry.cennso.com/cennso/pg-db
    tag: v18.3-2
  imagePullSecretRef:
    name: cennso-pull-secret
```

The **spec.imagePullSecretRef.name** refers to a Kubernetes Secret that must exist on the target cluster in the same Kubernetes Namespace as the Job.
