Use and Seed SAP BTP PostgreSQL in SAP BTP, Kyma Runtime
- How to use a Kyma Service Binding Secret that points to an SAP BTP PostgreSQL instance
- How to seed the PostgreSQL database with sample schema and data using a Kubernetes Job
Prerequisites
In this tutorial, you will provision a managed PostgreSQL instance on SAP BTP, bind it to your Kyma workload namespace, configure network access, and seed the database with a sample schema and data using a Kubernetes Job.
- Step 1
-
Go to the kyma-runtime-samples repository. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
-
Use the green Code button to choose one of the options to download the code locally, or simply run the following command using your CLI at your desired folder location:
Shell/BashCopygit clone https://github.com/SAP-samples/kyma-runtime-samples
-
- Step 2
-
Open the
database-postgresdirectory in your desired editor. -
Explore the content of the sample.
Within the
k8sfolder you can findseed-job.yaml, which includes a ConfigMap containing the SQL and a Kubernetes Job that runs thepsqlclient (imagepostgres:15). The Job expects the PostgreSQL connection details to come from a Service Binding Secret namedpostgres-binding. Adjust the Secret name and key names to match your binding if they differ.
-
- Step 3
-
From your subaccount overview in the SAP BTP cockpit, go to Entitlements and choose Edit.
-
Choose Add Service Plans and search for PostgreSQL, Hyperscaler Option.
-
Select free, and click Add 1 Service Plan.
NOTE: The available service plans depend on your account configuration. If the free plan is not available, select the plan that matches your subaccount entitlements.
-
Choose Save.
-
- Step 4
-
Create the
devnamespace and enableIstio:Shell/BashCopykubectl create namespace dev kubectl label namespaces dev istio-injection=enabledNamespaces separate objects inside a Kubernetes cluster. Choosing a different namespace requires adjustments to the provided samples.
Adding the
istio-injection=enabledlabel to the namespace enablesIstio.Istiois the service mesh implementation used by SAP BTP, Kyma runtime. -
Use the provided
postgres-instance-binding.yamlmanifest to create the PostgreSQL instance and binding:NOTE: The
postgres-instance-binding.yamlfile usesfreeas theservicePlanName. If the free plan is not available in your subaccount, open the file and change theservicePlanNamevalue to match the plan you entitled in the previous step.Shell/BashCopykubectl -n dev apply -f ./k8s/postgres-instance-binding.yaml -
It takes some time for the instance and binding to get created. To see if they are in the
Createdstate, run:Shell/BashCopykubectl -n dev get serviceinstance postgres-instance kubectl -n dev get servicebinding postgres-bindingOnce ready, both resources show
Createdin theSTATUScolumn:
-
- Step 5
- Patch the
ServiceInstanceyou applied above (namespace and name may differ) so it includes both your public IP and the Kyma NAT IP:Shell/BashCopyMY_IP=$(curl -s https://api.ipify.org) KYMA_NAT_IPS=$(kubectl --namespace kyma-system get configmap kyma-info -o json | jq -r '.data["cloud.natGatewayIps"]') kubectl -n dev patch serviceinstance postgres-instance --type=merge \ -p "{\"spec\":{\"parameters\":{\"allow_access\":\"${MY_IP},${KYMA_NAT_IPS}\"}}}" - It takes some time before the changes are applied. To see if the instance is updated, run:
Shell/BashCopy
kubectl -n dev get serviceinstance postgres-instance
- Patch the
- Step 6
-
Apply the ConfigMap and Job to seed the database. Run the following commands from the
database-postgresqldirectory using your CLI:Shell/BashCopykubectl -n dev apply -f ./k8s/seed-job.yaml kubectl -n dev get jobs seed-postgresql -
Wait until the Job shows
1/1in theCOMPLETIONScolumn:NAME COMPLETIONS DURATION AGE seed-postgresql 1/1 12s 30s
-
- Step 7
-
Run a temporary Pod that maps the Service Binding Secret keys to
PGHOST,PGPORT,PGDATABASE,PGUSER,PGPASSWORD, andPGSSLMODEand executes a query. Replace the Secret name and keys if your binding differs.Shell/BashCopykubectl -n dev apply -f - <<'EOF' apiVersion: v1 kind: Pod metadata: name: pg-client spec: restartPolicy: Never containers: - name: psql image: postgres:15 env: - name: PGHOST valueFrom: secretKeyRef: name: postgres-binding key: hostname - name: PGPORT valueFrom: secretKeyRef: name: postgres-binding key: port - name: PGDATABASE valueFrom: secretKeyRef: name: postgres-binding key: dbname - name: PGUSER valueFrom: secretKeyRef: name: postgres-binding key: username - name: PGPASSWORD valueFrom: secretKeyRef: name: postgres-binding key: password - name: PGSSLMODE valueFrom: secretKeyRef: name: postgres-binding key: sslmode optional: true command: ["psql"] args: ["-v", "ON_ERROR_STOP=1", "-c", "SELECT order_id, description, created FROM orders;"] EOF -
Check the Pod logs.
Shell/BashCopykubectl -n dev logs pod/pg-clientYou should see a table with two sample orders:
order_id | description | created ----------+--------------+------------------------- 10000001 | Sample Order 1 | 2024-01-01 00:00:00+00 10000002 | Sample Order 2 | 2024-01-01 00:00:00+00 (2 rows) -
If you want to delete the Pod, run:
Shell/BashCopykubectl -n dev delete pod/pg-client
-
- Step 8
If you want to remove the seeding assets, run:
Shell/BashCopykubectl -n dev delete job seed-postgresql kubectl -n dev delete configmap postgresql-sample-sqlThe PostgreSQL instance itself remains running on SAP BTP and can now be consumed by your Kyma workloads.