For AI agents: this documentation is indexed for you in llms.txt, and all of it in /llms.txt. Every page is also available as markdown: append .md to its address.

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 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 SecretOpens in a new tab on the cluster where the database instance is deployed. The Secret must hold the password for the new user account. For example:

    yaml

    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
  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

    Note

    Omit spec.connectionSecretRef when the target PostgreSQLInstance is deployed on the Management Cluster. For more information, see the Cennso PostgreSQL User document.

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

    bash

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 PRIVILEGESOpens in a new tab grants all privileges available for the object typeOpens in a new tab. To give the user full control of a database, use spec.databaseOwner instead. For more information, see the Cennso PostgreSQL User document.

You can control the behaviour and lifetime of the Kubernetes Job generated by this CR using the standard configuration options of the Kubernetes JobOpens in a new tab:

  • spec.backoffLimit
  • spec.ttlSecondsAfterFinished
  • spec.activeDeadlineSeconds

For more information about available configuration options, see the Cennso PostgreSQL User 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

    {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

    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 userOpens in a new tab by running the following SQL command:

    sql

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

  4. Grant the required privilegesOpens in a new tab to the user based on your application needs. For a list of available privileges, see PostgreSQL privileges documentationOpens in a new tab.

    Example command to grant all privileges on a specific database:

    sql

    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