Skip to content
Docs

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.

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.

A minimal PostgreSQLInstance needs only a disk size and an instance type:

apiVersion: database.entigo.com/v1alpha1
kind: PostgreSQLInstance
metadata:
name: example-postgresql
spec:
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:

Terminal window
kubectl get pginstances.database.entigo.com example-postgresql -w
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/v1alpha1
kind: PostgreSQLInstance
metadata:
name: example-postgresql
spec:
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-reboot

For every field, see the PostgreSQLInstance API reference.

A PostgreSQLUser is required if you create a PostgreSQLDatabase, because every database needs an owner. Otherwise it is optional.

apiVersion: database.entigo.com/v1alpha1
kind: PostgreSQLUser
metadata:
name: example-user
spec:
instanceRef:
name: example-postgresql
---
# A role that owns the database but cannot log in
apiVersion: database.entigo.com/v1alpha1
kind: PostgreSQLUser
metadata:
name: example-owner
spec:
instanceRef:
name: example-postgresql
login: false

Each user’s credentials are stored in the Kubernetes Secret <spec.instanceRef.name>-<metadata.name>, for example example-postgresql-example-user.

Terminal window
kubectl get postgresqlusers.database.entigo.com -w

For every field, see the PostgreSQLUser API reference.

A PostgreSQLDatabase is optional. Extensions are installed when the database is created.

apiVersion: database.entigo.com/v1alpha1
kind: PostgreSQLDatabase
metadata:
name: example-database
spec:
owner: example-owner
instanceRef:
name: example-postgresql
extensions: # optional
- postgis
- postgis_topology
extensionConfig: # optional
postgis_topology:
schema: topology
Terminal window
kubectl get postgresqldatabases.database.entigo.com example-database -w

For every field, see the PostgreSQLDatabase API reference.

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.

# Example
apiVersion: v1
kind: Pod
metadata:
name: postgres
spec:
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'

All three resources show SYNCED and READY as True:

Terminal window
$ kubectl get pginstances.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-postgresql True True pginstances.database.entigo.com 19m
$ kubectl get postgresqlusers.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-owner True True postgresqlusers.database.entigo.com 18m
example-user True True postgresqlusers.database.entigo.com 18m
$ kubectl get postgresqldatabases.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-database True True postgresqldatabases.database.entigo.com 17m

The RDS instance appears in the AWS Console:

One Secret per user, plus dbadmin:

$ kubectl get secret
NAME TYPE DATA AGE
example-postgresql-dbadmin Opaque 4 14m
example-postgresql-example-owner connection.crossplane.io/v1alpha1 4 14m
example-postgresql-example-user connection.crossplane.io/v1alpha1 4 14m
$ kubectl get secret example-postgresql-dbadmin -o yaml
apiVersion: v1
kind: Secret
metadata:
name: example-postgresql-dbadmin
namespace: <namespace>
type: Opaque
data:
endpoint: <base64-encoded-endpoint>
port: <base64-encoded-port>
username: <base64-encoded-username>
password: <base64-encoded-password>
$ kubectl get secret example-postgresql-example-user -o yaml
apiVersion: v1
kind: Secret
metadata:
name: example-postgresql-example-user
namespace: <namespace>
type: Opaque
data:
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:

Open a terminal in the pod and connect to the database using the mounted credentials.

Terminal window
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
postgres 1/1 Running 0 24s
$ kubectl exec -it postgres -- bash
postgres:/# env | grep ^PG
PGPORT=5432
PGPASSWORD=...
PGSSLMODE=require
PGUSER=example-user
PGHOST=example-postgresql-instance-...rds.amazonaws.com
postgres:/# psql example-database

Verify 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 functions
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.

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.

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/v1alpha1
kind: PostgreSQLInstance
metadata:
name: example-postgresql
spec:
backupRetentionPeriod: 7

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.

steps:
...
- name: apps
type: argocd-apps
modules:
...
- name: platform-apis
source: platform-apis
inputs:
platform-apis:
database:
environmentConfig:
backupRetentionPeriod: 14
postgresBackupBeforeDeletion: true