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

# Cennso PostgreSQL User

> [!NOTE]
> `PostgreSQLUser` only creates the user, applies the requested grants, and sets the database owner. It does not update or delete them. Deleting a `PostgreSQLUser` Custom Resource does not drop the user from PostgreSQL, and changing the password in the referenced Kubernetes Secret does not change the password of a user that already exists. To drop the existing user, use the [DROP ROLE](https://www.postgresql.org/docs/current/sql-droprole.html) SQL statement.
>
> Before provisioning a user, 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.

A Cennso PostgreSQL User is represented by the `PostgreSQLUser` Custom Resource (CR). Applying this CR to a Management Cluster creates a Kubernetes Job that provisions a PostgreSQL user on an existing [Cennso PostgreSQL Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md) and applies the requested `GRANT` statement. In PostgreSQL terms, a user is a [role](https://www.postgresql.org/docs/18/user-manag.html) with the `LOGIN` attribute.

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

## Kubernetes cluster secret reference

To deploy the Cennso PostgreSQL User 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 user 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 User 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 user 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 user-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.

## User identity

To define the PostgreSQL user that should be created, use the following configuration parameter:

```yaml
spec:
  username: {USERNAME}
```

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

## Password secret reference

> [!IMPORTANT]
> The Kubernetes secret with the user's password must be provisioned before you apply the `PostgreSQLUser` CR on your Management Cluster.

The password for the new user is read from a Kubernetes Secret on the target cluster, in the same Kubernetes Namespace as the provisioning Job. You can configure it using the following parameters:

```yaml
spec:
  passwordSecretRef:
    name: {PASSWORD-SECRET-NAME}
    key: password
```

The **spec.passwordSecretRef.name** is the name of the Kubernetes Secret that holds the user's password.

The **spec.passwordSecretRef.key** is the key within the Secret whose value is used as the password. The default value of **spec.passwordSecretRef.key** is `password`.

## Grants

To configure the privileges of the new user, use the following configuration options:

- **spec.privileges**
- **spec.grantScope**
- **spec.target**

All three combine into a single [GRANT](https://www.postgresql.org/docs/current/sql-grant.html) statement:

```yaml
spec:
  privileges:
    - SELECT
    - INSERT
    - UPDATE
  grantScope: TABLE
  target: my_table
```

The example above produces `GRANT SELECT, INSERT, UPDATE ON TABLE my_table TO {USERNAME};`.

The **spec.privileges** is the list of PostgreSQL privileges to grant. The privileges must be valid for the object kind set by **spec.grantScope**, otherwise the Job fails:

| Grant scope | Privileges |
|---|---|
| `DATABASE` | `CREATE`, `CONNECT`, `TEMPORARY` |
| `SCHEMA` | `CREATE`, `USAGE` |
| `TABLE` | `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, `REFERENCES`, `TRIGGER`, `MAINTAIN` |

`ALL PRIVILEGES` grants every privilege that applies to the object kind. For the meaning of each privilege, see the [PostgreSQL privileges documentation](https://www.postgresql.org/docs/current/ddl-priv.html).

The **spec.grantScope** configures the kind of PostgreSQL object that **spec.target** identifies. The supported values are:

- `DATABASE`
- `SCHEMA`
- `TABLE`

A `SCHEMA` or `TABLE` must be in the default database of the instance, because the provisioning Job connects to that database. To give the user its own database, use [**spec.databaseOwner**](#database-ownership) instead.

The **spec.target** is the name of the PostgreSQL object the privileges are granted on. It must be a plain, unqualified identifier of up to `64` characters, built from letters, digits, or underscores. Schema-qualified targets such as `myschema.mytable` are not supported.

Both **spec.grantScope** and **spec.target** are required whenever **spec.privileges** is not empty, and the Kubernetes API server rejects a resource that sets privileges without them. If **spec.privileges** is empty or omitted, the user is created without any additional privileges.

## Database ownership

To make the new user the owner of an existing logical database, use the following configuration option:

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

The owner has full control of the database, including its [`public`](https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PUBLIC) schema. For more information about the ownership, see the [official documentation](https://www.postgresql.org/docs/current/ddl-priv.html).

The **spec.databaseOwner** must be a plain identifier of up to `64` characters, built from letters, digits, or underscores.

> [!NOTE]
> The database referenced by **spec.databaseOwner** or by a `DATABASE`-scoped grant must be created before you apply the `PostgreSQLUser`, for example with a [Cennso PostgreSQL Schema](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-schema.md). If the database does not exist when the Job runs, the Job fails.

## Job retries, lifetime, and deadline

To control how the user-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. 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 `PostgreSQLUser` exists. After **spec.ttlSecondsAfterFinished** expires, the Job is deleted and then created again. The new Job run does not change the user because it creates the user only if it does not already exist. However, it reapplies the grants and 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 `PostgreSQLUser` becomes `Ready` when the Job has completed. A `PostgreSQLUser` 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 {POSTGRESQLUSER-NAME}-user
kubectl -n {POSTGRESQL-CLUSTER-NAMESPACE} logs job/{POSTGRESQLUSER-NAME}-user
```

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 {POSTGRESQLUSER-NAME}-user
```

The same applies after changing **spec.image**, **spec.restartPolicy**, **spec.username**, **spec.privileges**, **spec.grantScope**, **spec.target**, or **spec.databaseOwner** of an existing `PostgreSQLUser`. 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 user-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.
