Deploy a CAP Application with PostgreSQL in SAP BTP, Kyma Runtime
- How to add and configure PostgreSQL to your Kyma environment.
- How to deploy a CAP application in your Kyma environment.
Prerequisites
- SAP BTP, Kyma runtime enabled
- kubectl installed
- Node.js and cds-dk installed
- Docker installed
- Sufficient quota to add PostgreSQL entitlements in your environment
In this tutorial, you will add PostgreSQL entitlements to your subaccount and deploy a CAP Application in the Kyma environment.
- Step 1
-
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 choose 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.
You are now ready to deploy a PostgreSQL instance in your subaccount.
For the sake of this tutorial, the chosen entitlement units are sufficient. However, when sizing and configuring your environment, consider the necessary amount of entitlement units (2GB/4GB memory blocks and 5GB storage blocks) to support your architecture. Depending on the hyperscaler, configurations can consume different numbers of entitlement units. See Sizing and Service Plans and Entitlements.
-
- Step 2
-
Prepare your sample project:
bashCopycds init bookshop --add sample,nodejs && cd bookshop -
Go to
db/schema.cdsand fordescrchangelocalized Stringtolocalized LargeString.cdsCopyentity Books : managed { key ID : Integer; author : Association to Authors @mandatory; title : localized String @mandatory; descr : localized LargeString; genre : Association to Genres; stock : Integer; price : Price; currency : Currency; }
-
- Step 3
-
Add PostgreSQL deployment configuration:
bashCopycds add postgres --for productionTIP: For more information, see Add PostgreSQL Deployment Configuration.
-
In
package.json, change[production].modelfromdb/hanatodb/postgresand add theauthsection:jsonCopy"cds": { "requires": { "db-ext": { "[development]": { "model": "db/sqlite" }, "[production]": { "model": "db/postgres" } }, "[production]": { "db": "postgres" }, "auth": { "[production]": { "kind": "dummy" } } } }With the dummy authentication, you pass all the authorization checks. It’s intended only for development and should not be used in production environments. For more information, see Dummy Authentication.
-
- Step 4
-
Generate the Helm chart structure:
bashCopycds add kyma -
When prompted, provide your registry username and cluster domain, for example:
- registry server: user123
- cluster domain: abc123.kyma.ondemand.com
TIP: To get your Kyma cluster domain, run:
kubectl get gateways.networking.istio.io -n kyma-system kyma-gateway \ -o jsonpath='{.spec.servers[0].hosts[0]}'This creates:
- chart/Chart.yaml – Helm chart metadata
- chart/values.yaml – Configuration file
- containerize.yaml – Configuration for building and publishing container images
-
In your
values.yamlfile, changepostgres-db-deployertopostgres-deployerand changepostgres.servicePlanNameto match the service plan you added in the entitlements step.yamlCopy... postgres-deployer: image: repository: bookshop-postgres-deployer bindings: postgres: serviceInstanceName: postgres ... postgres: serviceOfferingName: postgresql-db servicePlanName: free ...NOTE: The
servicePlanNamevalue must match the service plan available in your account. Replacefreewith the plan you selected in the entitlements step if different. -
If you want to connect to the PostgreSQL instance during local development, add your static internet IP address to the
allow_accessparameter invalues.yaml. The Kyma NAT IP Gateway is already added bycds add kyma. Append your static IP after a comma:yamlCopypostgres: serviceOfferingName: postgresql-db servicePlanName: free parameters: allow_access: "<kyma-nat-ip-gateway>,<your-static-internet-ip-address>"TIP: To find your public IP address, you can run
curl ifconfig.mein your terminal.
-
- Step 5
-
Create the
cap-bookstorenamespace and enableistio:bashCopykubectl create namespace cap-bookstore kubectl label namespaces cap-bookstore istio-injection=enabled -
Deploy your application to Kyma:
bashCopycds up -2 k8s --namespace cap-bookstoreYou might get a warning that your registry server is invalid. If so, simply enter your Docker username again.
-
Create an image pull secret when prompted.
This command executes the following actions:
- Builds your CAP application (
cds build --production). - Generates all necessary Helm templates in
gen/chart/templates/. - Creates container images for srv and database deployer using Cloud Native Buildpacks.
- Pushes images to your container registry (configured in
containerize.yaml). - Deploys using Helm to your Kyma cluster.
- Runs the database deployer Job to initialize PostgreSQL with the schema and CSV data.
NOTE: The deployment process might timeout, especially during the first deployment when images are being built and pushed. If this happens, the deployment will continue in the background. You can check the status using:
kubectl get pods -n cap-bookstoreWait until all Pods show
Runningstatus before proceeding to verification. - Builds your CAP application (
-
- Step 6
- In Kyma dashboard, go to your
cap-bookstorenamespace, and select API Rules. - Choose
bookshop-srvand select the link underHostsfrom the Status section. - Add
/odata/v4/catalog/Booksat the end of the link. You should see your books and authors.
Congratulations! You have now deployed your CAP application with PostgreSQL in SAP BTP, Kyma runtime.
- In Kyma dashboard, go to your