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

# Create a database

> [!IMPORTANT]
> To create a database, your PostgreSQL user must have the [`CREATEDB`](https://www.postgresql.org/docs/18/role-attributes.html) privilege. The default user provisioned automatically by `PostgreSQLInstance` has this privilege. For more information about the database users, see the [Create a database user](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-users-creation.md) guide.

After you deploy the PostgreSQL cluster using the `PostgreSQLInstance` Custom Resource, you can create logical databases on this cluster. The Cennso Database extension lets you provision a logical database either declaratively with the [`PostgreSQLSchema` Custom Resource](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-postgresql/xpostgresqlschema_definition.md), or manually. Both methods are described below.

## Provision a database using `PostgreSQLSchema` CR

To create a database using the `PostgreSQLSchema` Custom Resource, create a resource with the database name you want to create and apply it to the Management Cluster. For example:

```yaml
apiVersion: db.contrib.cennso.com/v1alpha1
kind: PostgreSQLSchema
metadata:
  name: {POSTGRESQLSCHEMA-NAME}
  namespace: cennso-infra
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: cennso-infra
  namespace: {NAMESPACE-WITH-DATABASE-CLUSTER}
  dbInstanceName: {POSTGRESQLINSTANCE-NAME}
  schemaName: {DATABASE-NAME}
  imagePullSecretRef:
    name: custom-pull-secret
  image:
    registry: registry.cennso.com/cennso/pg-db
    tag: v18.3-2
```

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

You can apply the `PostgreSQLSchema` Custom Resource to your Management Cluster using the following command:

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

After you apply the `PostgreSQLSchema` 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 logical database named `{DATABASE-NAME}` in the `{NAMESPACE-WITH-DATABASE-CLUSTER}` namespace.

To create a database 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.

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

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 Schema](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-schema.md) document.

## Provision a database manually

To create a database, execute the following steps:

1. Find the primary PostgreSQL instance. The primary PostgreSQL instance Pod must have the `postgres-operator.crunchydata.com/role` label set to `primary`. You can find the Pod with the primary PostgreSQL instance using the following command:

   ```bash
   kubectl get pods --selector postgres-operator.crunchydata.com/role=primary -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

2. Open a shell in the primary PostgreSQL instance container:

   ```bash
   kubectl exec -it {PRIMARY-POSTGRESQL-INSTANCE-POD} -c database -n {NAMESPACE-WITH-DATABASE-CLUSTER} -- bash
   ```

3. Connect to the primary PostgreSQL instance:

   ```bash
   psql -h localhost -U {USER-NAME} -d {POSTGRESQLINSTANCE-NAME}
   ```

   Enter the password of `{USER-NAME}` when prompted.

4. Create a database using the `CREATE DATABASE` SQL statement. For example, you can use the following SQL statement to create a database with the `cennso_database` name:

   ```SQL
   CREATE DATABASE cennso_database;
   ```

For more information, see the [official PostgreSQL documentation](https://www.postgresql.org/docs/18/sql-createdatabase.html).
