How to set up your CAP project with PostgreSQL database?

XAP Software · May 5, 2022

In case you need to create CAP project with PostgreSQL database, this article will help you understand the initial setup.

cds-pg

CAP projects cannot work natively with PostgreSQL. To connect to such database you need to install community-driven cds-pg and cds-dbm

$ npm install cds-pg cds-dbm

Database connect in package.json


"cds": {
        "requires": {
            "db": {
                "kind": "database"
              },
              "database": {
                "dialect": "plain",
                "impl": "cds-pg",
                "model": [
                  "srv"
                ]
              }
        },

Data for connecting to the database is written in default-env.json


{
    "VCAP_SERVICES": {
      "postgres": [
        {
          "name": "postgres",
          "label": "postgres",
          "tags": ["plain", "database"],
          "credentials": {
            "host": "localhost",
            "port": 5432,
            "database": "database",
            "user": "user",
            "password": "password",
            "schema":"schema"
          }
        }
      ]
    }
  }

Now we should define our data model and services.

cds-dpm

Use cds-dbm to deploy database artifacts to the PostgreSQL database.

$ npx cds-dbm deploy

Kirill Verbin

Twitter