Getting Started with the Databases Service¶
This guide will walk you through the process of provisioning a new PostgreSQL database using the managed Databases service.
By following these steps, you will learn how to find the available database
server, create a Database custom resource to provision your database, and
connect your application to it using the automatically generated credentials.
Prerequisites¶
Before you begin, you will need access to a Kubernetes cluster on the platform.
Step 1: Discover the Database Instance¶
The platform provides one or more highly available database servers (instances) per cluster. You do not create the server; you create your individual databases on this shared server.
First, you need to find the name of the available database instance. You can do
this with kubectl:
You will see output similar to this, listing the available instance(s):
NAME ENGINE VERSION MANAGED DATABASES COUNT OPERATOR VERSION READY
prod1-dc4-dbaas01.netic-platform.shared.k8s.netic.dk postgres 16.4 42 2.45.1 true
Take note of this name. You will need it for the next step.
Step 2: Create Your Database¶
You provision a new database by creating a Database custom
resource in your application's namespace. The db-operator will detect this
resource and automatically create the database and a corresponding user and
password.
Create a manifest file (e.g., my-database.yaml) with the following content:
apiVersion: "kinda.rocks/v1beta1"
kind: "Database"
metadata:
name: "my-application-db"
spec:
# This must match the name of the DbInstance from Step 1
instance: prod1-dc4-dbaas01.netic-platform.shared.k8s.netic.dk
# The operator will create a Secret with this name
secretName: my-app-db-credentials
# This prevents the database from being deleted if the manifest is removed
deletionProtected: true
# Leave spec.backup unset. Backup is managed at the instance level.
Apply this manifest to your cluster using kubectl or by committing it to your
GitOps repository.
Key Configuration Fields¶
spec.instance: (Required) The full name of theDbInstanceyou discovered in Step 1.spec.secretName: (Required) The name of the KubernetesSecretthat the operator will create to hold the database credentials (database name, user name and password). The operator also creates aConfigMapwith the same name that holds the server host and port.spec.deletionProtected: (Highly Recommended) When set totrue, deleting theDatabasemanifest from Kubernetes will not delete the actual database on the server. This is a critical safety feature to prevent accidental data loss.
For every field you can set, see the Database reference.
Step 3: Access and Use the Database¶
Once the db-operator has processed your request, it will create a Secret and
a ConfigMap with the name you specified in secretName.
You can inspect the secret to see the credentials:
The best way to use these values is to expose the Secret and the ConfigMap
as environment variables in your application's Deployment or StatefulSet.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-application
spec:
# ... other deployment specs
template:
spec:
containers:
- name: app
image: my-app-image
envFrom:
- secretRef:
name: my-app-db-credentials
- configMapRef:
name: my-app-db-credentials
# Your application can now use the environment variables
# POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD, CONNECTION_STRING,
# DB_CONN (host) and DB_PORT to connect to the database.
Advanced: Custom Connection Strings¶
The Secret created by the operator contains all the individual fields needed
to connect to the database. If your application requires a specific connection
string format (e.g., a JDBC URL), you can instruct the operator to generate it
for you.
Add the spec.credentials.templates section to your Database manifest:
spec:
# ... other fields
credentials:
templates:
- name: JDBC_URL # The key for the new entry in the Secret
secret: true # Write the entry to the Secret, not the ConfigMap
# A Go template for your desired connection string format
template: "jdbc:{{ .Protocol }}://{{ .Hostname }}:{{ .Port }}/{{ .Database }}?user={{ .Username }}&password={{ .Password }}"
The operator will add a new key (JDBC_URL) to the Secret with the fully
rendered connection string.
When you set spec.credentials.templates, the operator no longer adds the
default CONNECTION_STRING entry. Add it to the list as well if your
application uses it. For the available template fields and functions, see the
Database reference.
Backup and Recovery¶
Database backup and recovery is managed at the instance level by the platform team. You do not need to configure anything in your manifests.
Full backups are taken nightly at 22:00 and archived for 6 months. Point-in-time recovery (PITR) is also enabled, with transaction logs archived every 5 minutes. This allows for restoration to any 5-minute interval within the last 5 days.
To initiate a database restoration, follow Requesting a Database Restore.
Next Steps¶
- Reference:
DatabaseCRD lists every field you can set on aDatabase. - Reference:
DbUserCRD shows how to create additional users, for example a read-only user, on your database.