> 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/operation-guides/cennso-postgresql-users-creation.md).

# Create a database user

By default, after bootstrapping Cennso PostgreSQL instances, only system users and the default user named after the `PostgreSQLInstance` are created. These users should be used only for tasks such as administration, monitoring, and database backups. To create additional database user accounts for your applications, you can either use the [`PostgreSQLUser` Custom Resource](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-postgresql/xpostgresqluser_definition.md) or create them manually. Both methods are described below.

## Provision a user account using `PostgreSQLUser` CR

The creation of a user account consists of two steps:

1. Create a Kubernetes [Secret](https://kubernetes.io/docs/concepts/configuration/secret/) on the cluster where the database instance is deployed. The Secret must hold the password for the new user account. For example:

   ```yaml
   apiVersion: v1
   kind: Secret
   metadata:
     name: my-user-password
   data:
     password: ZXhhbXBsZS1wYXNzd29yZA==
   type: Opaque
   ```

   The Secret must be created in the same Kubernetes Namespace where the database instance is deployed. You can create the Secret using the following command:

   ```bash
   kubectl apply -f user-secret.yaml -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

2. Create the manifest (for example `postgresql-user.yaml`) with the user account specified by the `PostgreSQLUser` Custom Resource on the Management Cluster, referencing the Secret you just created:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: PostgreSQLUser
   metadata:
     name: {POSTGRESQLUSER-NAME}
     namespace: cennso-infra
   spec:
     connectionSecretRef:
       name: {CORE-CLUSTER-NAME}
       namespace: cennso-infra
     namespace: {NAMESPACE-WITH-DATABASE-CLUSTER}
     dbInstanceName: {POSTGRESQLINSTANCE-NAME}
     username: {USER-NAME}
     passwordSecretRef:
       name: my-user-password
       key: password
     privileges: ["ALL PRIVILEGES"]
     grantScope: DATABASE
     target: example
   ```

   > [!NOTE]
   > Omit **spec.connectionSecretRef** when the target `PostgreSQLInstance` is deployed on the Management Cluster. For more information, see the [Cennso PostgreSQL User](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-user.md#kubernetes-cluster-secret-reference) document.

3. Apply the `PostgreSQLUser` Custom Resource to your Management Cluster:

   ```bash
   kubectl apply -f postgresql-user.yaml
   ```

After you apply the `PostgreSQLUser` manifest, a Kubernetes Job is created on the Core Cluster specified by `{CORE-CLUSTER-NAME}`, or on the Management Cluster if **spec.connectionSecretRef** is skipped. This Job creates a new user account with `{USER-NAME}` name in the `{NAMESPACE-WITH-DATABASE-CLUSTER}` namespace, using the password from the `my-user-password` Kubernetes Secret and privileges from the manifest.

To create a user account at the given database instance, the Job must be able to find the database cluster. The **spec.dbInstanceName** field is used for this purpose. Set it to the **metadata.name** of the `PostgreSQLInstance` resource related to the target database cluster.

PostgreSQL grants privileges on one kind of object at a time, so you must set the kind of the object using the **spec.grantScope** parameter whenever you set **spec.privileges**. Supported values are:

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

The **spec.target** parameter defines the name of the database, schema, or table on which the privileges from **spec.privileges** are granted.

According to the manifest above, the new user account is created with `ALL PRIVILEGES` on the `example` database.

> [!NOTE]
> [`ALL PRIVILEGES`](https://www.postgresql.org/docs/18/sql-grant.html) grants all privileges [available for the object type](https://www.postgresql.org/docs/18/ddl-priv.html). To give the user full control of a database, use **spec.databaseOwner** instead. For more information, see the [Cennso PostgreSQL User](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-user.md#database-ownership) document.

You can control the behaviour and lifetime of the Kubernetes Job generated by this CR using the standard configuration options of the [Kubernetes Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/):

- **spec.backoffLimit**
- **spec.ttlSecondsAfterFinished**
- **spec.activeDeadlineSeconds**

For more information about available configuration options, see the [Cennso PostgreSQL User](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-user.md) document.

## Provision a user manually

To provision a new user account manually, follow these steps:

1. Get the credentials for the default user by running the following command:

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

   `{NAMESPACE}` is the Kubernetes Namespace where the Cennso PostgreSQL is deployed, and `{POSTGRESQLINSTANCE-NAME}` is the **metadata.name** of the `PostgreSQLInstance`.

2. Connect to the PostgreSQL primary instance using the default role credentials:

   ```bash
   psql -h {POSTGRESQL-PRIMARY-INSTANCE} -U {POSTGRESQLINSTANCE-NAME} -d {POSTGRESQLINSTANCE-NAME}
   ```

   Enter the password when prompted. `{POSTGRESQL-PRIMARY-INSTANCE}` is the address of the primary PostgreSQL instance, for example the `{POSTGRESQLINSTANCE-NAME}-primary.{NAMESPACE}.svc` Kubernetes Service.

3. [Create a new database user](https://www.postgresql.org/docs/18/sql-createrole.html) by running the following SQL command:

   ```sql
   CREATE ROLE {USERNAME} WITH LOGIN PASSWORD '{PASSWORD}';
   ```

   Replace `{USERNAME}` with the desired username. Replace `{PASSWORD}` with the desired password.

4. [Grant the required privileges](https://www.postgresql.org/docs/18/sql-grant.html) to the user based on your application needs. For a list of available privileges, see [PostgreSQL privileges documentation](https://www.postgresql.org/docs/18/ddl-priv.html).

   Example command to grant all privileges on a specific database:

   ```sql
   GRANT ALL PRIVILEGES ON DATABASE {DATABASE} TO {USERNAME};
   ```

   Replace `{DATABASE}` with your database name and `{USERNAME}` with the username created in step 3.

For example, to create a user with all privileges on the `example` database, you can use the following SQL commands:

```sql
CREATE ROLE example WITH LOGIN PASSWORD 'example';
GRANT ALL PRIVILEGES ON DATABASE example TO example;
```
