Create a PostgreSQL Instance
Create a PostgreSQL instance, users and databases, and mount their credentials in a Pod.
On this page
- Prerequisites
- 1. Create the instance
- Optional settings
- 2. Create a user
- 3. Create a database
- 4. Mount the credentials in a Pod
- 5. Verify
- Resources are ready
- Credentials are stored
- The Pod can connect
- Troubleshooting
- EnvironmentConfig
- backupRetentionPeriod
- postgresBackupBeforeDeletion
- Infralib Configuration Example
This guide creates an Amazon RDS for PostgreSQL instance from Kubernetes, adds a user and a database to it, and mounts the connection credentials in a Pod.
You describe the database with three manifests: PostgreSQLInstance, PostgreSQLUser and PostgreSQLDatabase. The platform creates the RDS instance, the security group rules that let Pods reach it, and a Kubernetes Secret with connection credentials for each user. Daily backups and a final snapshot on deletion are turned on for every instance, see EnvironmentConfig.
It is good practice to include the manifests in your application’s Helm chart, so the database is deployed together with the application that uses it.
Prerequisites
Section titled “Prerequisites”PostgreSQL is enabled by default. Your Infralib configuration needs:
| Module | Flag | Default | What it does |
|---|---|---|---|
platform-apis |
platform-apis.postgresql.install |
true |
Installs the PostgreSQLInstance, PostgreSQLUser and PostgreSQLDatabase APIs. |
crossplane-aws |
global.createObservers, global.createNamespacedObservers |
false |
Installs the RDS and EC2 providers and the VPC lookups the instance needs. Set both to true. |
crossplane-sql |
— | — | Creates users and databases inside the instance. Add the module to the argocd-apps step. |
For the base modules every platform API needs, see Infralib Requirements.
1. Create the instance
Section titled “1. Create the instance”A minimal PostgreSQLInstance needs only a disk size and an instance type:
apiVersion: database.entigo.com/v1alpha1kind: PostgreSQLInstancemetadata: name: example-postgresqlspec: allocatedStorage: 20 engineVersion: '17.10' instanceType: 'db.t3.small'The master user dbadmin is created with the instance. Its credentials are stored in the Kubernetes Secret <metadata.name>-dbadmin and in AWS Secrets Manager.
Creating the instance takes more than 5 minutes. Watch it until READY is True:
kubectl get pginstances.database.entigo.com example-postgresql -wOptional settings
Section titled “Optional settings”| Field | Default | What it does |
|---|---|---|
multiAZ |
false |
Runs a standby copy in a second availability zone. |
deletionProtection |
true |
Blocks deletion. Set to false before you delete the instance. |
iops |
unset | Reserved IOPS. Increasing the disk size is usually cheaper. |
backupRetentionPeriod |
14 |
Days of daily backups to keep, 0–35. Overrides the cluster default. |
backupWindow |
set by region | Daily backup time in UTC, for example '03:00-05:00'. |
maintenanceWindow |
set by AWS | Weekly maintenance time, for example 'Mon:00:00-Mon:03:00'. |
autoMinorVersionUpgrade |
true |
Applies minor engine upgrades in the maintenance window. |
allowMajorVersionUpgrade |
false |
Allows changing engineVersion to a new major version. |
parameterGroupName |
unset | Uses an existing RDS parameter group, for example 'default.postgres17'. |
parameterGroupParameters |
unset | Sets individual PostgreSQL parameters. |
For example, a production instance with a standby and custom parameters:
apiVersion: database.entigo.com/v1alpha1kind: PostgreSQLInstancemetadata: name: example-postgresqlspec: allocatedStorage: 20 engineVersion: '17.10' instanceType: 'db.t3.small' multiAZ: true backupWindow: '03:00-05:00' maintenanceWindow: 'Mon:00:00-Mon:03:00' parameterGroupParameters: max_connections: '10' applyMethod: pending-rebootFor every field, see the PostgreSQLInstance API reference.
2. Create a user
Section titled “2. Create a user”A PostgreSQLUser is required if you create a PostgreSQLDatabase, because every database needs an owner. Otherwise it is optional.
apiVersion: database.entigo.com/v1alpha1kind: PostgreSQLUsermetadata: name: example-userspec: instanceRef: name: example-postgresql---# A role that owns the database but cannot log inapiVersion: database.entigo.com/v1alpha1kind: PostgreSQLUsermetadata: name: example-ownerspec: instanceRef: name: example-postgresql login: falseEach user’s credentials are stored in the Kubernetes Secret <spec.instanceRef.name>-<metadata.name>, for example example-postgresql-example-user.
kubectl get postgresqlusers.database.entigo.com -wFor every field, see the PostgreSQLUser API reference.
3. Create a database
Section titled “3. Create a database”A PostgreSQLDatabase is optional. Extensions are installed when the database is created.
apiVersion: database.entigo.com/v1alpha1kind: PostgreSQLDatabasemetadata: name: example-databasespec: owner: example-owner instanceRef: name: example-postgresql extensions: # optional - postgis - postgis_topology extensionConfig: # optional postgis_topology: schema: topologykubectl get postgresqldatabases.database.entigo.com example-database -wFor every field, see the PostgreSQLDatabase API reference.
4. Mount the credentials in a Pod
Section titled “4. Mount the credentials in a Pod”Read the credentials from the user’s Secret into the standard PG* environment variables. Use the dbadmin Secret only for administration.
For more information about Secrets in Kubernetes, see Kubernetes documentation.
# ExampleapiVersion: v1kind: Podmetadata: name: postgresspec: terminationGracePeriodSeconds: 1 containers: - name: postgres image: postgres:alpine command: ['sleep', 'infinity'] env: - name: PGHOST valueFrom: secretKeyRef: name: example-postgresql-example-user key: endpoint - name: PGPORT valueFrom: secretKeyRef: name: example-postgresql-example-user key: port - name: PGUSER valueFrom: secretKeyRef: name: example-postgresql-example-user key: username - name: PGPASSWORD valueFrom: secretKeyRef: name: example-postgresql-example-user key: password - name: PGSSLMODE value: 'require'5. Verify
Section titled “5. Verify”Resources are ready
Section titled “Resources are ready”All three resources show SYNCED and READY as True:
$ kubectl get pginstances.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-postgresql True True pginstances.database.entigo.com 19m$ kubectl get postgresqlusers.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-owner True True postgresqlusers.database.entigo.com 18mexample-user True True postgresqlusers.database.entigo.com 18m$ kubectl get postgresqldatabases.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-database True True postgresqldatabases.database.entigo.com 17mThe RDS instance appears in the AWS Console:

Credentials are stored
Section titled “Credentials are stored”One Secret per user, plus dbadmin:
$ kubectl get secretNAME TYPE DATA AGEexample-postgresql-dbadmin Opaque 4 14mexample-postgresql-example-owner connection.crossplane.io/v1alpha1 4 14mexample-postgresql-example-user connection.crossplane.io/v1alpha1 4 14m
$ kubectl get secret example-postgresql-dbadmin -o yamlapiVersion: v1kind: Secretmetadata: name: example-postgresql-dbadmin namespace: <namespace>type: Opaquedata: endpoint: <base64-encoded-endpoint> port: <base64-encoded-port> username: <base64-encoded-username> password: <base64-encoded-password>
$ kubectl get secret example-postgresql-example-user -o yamlapiVersion: v1kind: Secretmetadata: name: example-postgresql-example-user namespace: <namespace>type: Opaquedata: endpoint: <base64-encoded-endpoint> port: <base64-encoded-port> username: <base64-encoded-username> password: <base64-encoded-password>The dbadmin credentials are also in AWS Secrets Manager:

The Pod can connect
Section titled “The Pod can connect”Open a terminal in the pod and connect to the database using the mounted credentials.
$ kubectl get podNAME READY STATUS RESTARTS AGEpostgres 1/1 Running 0 24s
$ kubectl exec -it postgres -- bashpostgres:/# env | grep ^PGPGPORT=5432PGPASSWORD=...PGSSLMODE=requirePGUSER=example-userPGHOST=example-postgresql-instance-...rds.amazonaws.com
postgres:/# psql example-databaseVerify the configuration is correct.
example-database=> \du List of roles Role name | Attributes-----------------+------------------------------------------------------------ dbadmin | Create role, Create DB + | Password valid until infinity example-owner | Cannot login example-user |...
example-database=> \l List of databases Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges------------------+---------------+----------+-----------------+-------------+-------------+--------+-----------+----------------------- example-database | example-owner | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | postgres | dbadmin | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |...
example-database=> \dx List of installed extensions Name | Version | Default version | Schema | Description------------------+---------+-----------------+------------+------------------------------------------------------------ plpgsql | 1.0 | 1.0 | pg_catalog | PL/pgSQL procedural language postgis | 3.5.0 | 3.5.0 | public | PostGIS geometry and geography spatial types and functions postgis_topology | 3.5.0 | 3.5.0 | topology | PostGIS topology spatial types and functionsTroubleshooting
Section titled “Troubleshooting”| Symptom | Cause / fix |
|---|---|
no matches for kind "PostgreSQLInstance" |
The API is not installed. Check the Prerequisites. |
SYNCED is False |
The platform could not create a cloud resource. kubectl describe pginstances.database.entigo.com <name> shows the error under Conditions and Events. |
READY stays False for several minutes after creation |
Normal. RDS takes more than 5 minutes to create an instance. |
A user or database never becomes READY |
The instance must be READY first. If it is, check that the crossplane-sql module is installed. |
| The instance is not deleted | deletionProtection is true by default. Set it to false, apply, then delete. |
EnvironmentConfig
Section titled “EnvironmentConfig”Backup behaviour is not configured on the PostgreSQLInstance object. It is set once for the whole cluster in the platform-apis EnvironmentConfig, so every database gets the same protection without developers having to think about it.
| Setting | Default | What it does |
|---|---|---|
backupRetentionPeriod |
14 |
How many days of automatic daily backups AWS keeps for every instance. |
postgresBackupBeforeDeletion |
true |
Takes a final snapshot before an instance is deleted, so the data can still be recovered. |
tags |
{} |
Cloud tags added to every database resource. See Resource Tagging. |
backupRetentionPeriod
Section titled “backupRetentionPeriod”The number of days AWS keeps automatic daily backups. A higher number means you can restore further back in time, at the cost of extra backup storage.
This is only a default. A single instance can use a different value through spec.backupRetentionPeriod:
apiVersion: database.entigo.com/v1alpha1kind: PostgreSQLInstancemetadata: name: example-postgresqlspec: backupRetentionPeriod: 7postgresBackupBeforeDeletion
Section titled “postgresBackupBeforeDeletion”When enabled, deleting a PostgreSQLInstance takes one last snapshot of the database before the instance disappears. That snapshot stays in AWS and can be used to bring the data back later, see Restore a PostgreSQL Instance.
Turning this off means a deleted instance is gone for good. Only do that in throwaway environments where the data does not matter.
Deleting an instance is a separate protection: instances have spec.deletionProtection enabled by default and must have it disabled before they can be deleted at all.
Infralib Configuration Example
Section titled “Infralib Configuration Example”steps: ... - name: apps type: argocd-apps modules: ... - name: platform-apis source: platform-apis inputs: platform-apis: database: environmentConfig: backupRetentionPeriod: 14 postgresBackupBeforeDeletion: true