> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add a database to Strapi

> After your Strapi application is deployed on Upsun Cloud, you might want to add and configure a service to your application.


This guide will show you how to provision and connect to two different databases on Upsun Cloud:

* PostgreSQL
* Oracle MySQL

## 1. Branch

Like all updates to your Upsun Cloud projects, first create a new dedicated environment to test this change.

```bash theme={null}
git checkout -b upgrade-db
```

## 2. Install the Node.js package

> [tab] PostgreSQL
>
> ```bash theme={null}
> yarn add pg
> ```

> [tab] Oracle MySQL
>
> ```bash theme={null}
> yarn add mysql
> ```

## 3. Add a new service

Add a new service to your `.upsun/config.yaml` file:

> [tab] PostgreSQL
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           type: "nodejs:\{\{version:nodejs:latest}}"
>
> &#x20;           \[...]
>
> &#x20;       services:
> &#x20;         database:
> &#x20;       	  type: postgresql:\{\{version:postgresql:latest}}

> [tab] Oracle MySQL
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           type: "nodejs:\{\{version:nodejs:latest}}"
>
> &#x20;           \[...]
>
> &#x20;       services:
> &#x20;         database:
> &#x20;       	  type: oracle-mysql:\{\{version:oracle-mysql:latest}}

## 4. Add a new relationship

Add a new relationship to your `.upsun/config.yaml` file to allow access to the new service

> [tab] PostgreSQL
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           type: "nodejs:\{\{version:nodejs:latest}}"
>
> &#x20;           \[...]
>
> &#x20;           relationships:
> &#x20;             database:
>
> &#x20;       services:
> &#x20;         database:
> &#x20;       	  type: postgresql:\{\{version:postgresql:latest}}

> [tab] Oracle MySQL
>
> &#x20;       applications:
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "/"
> &#x20;           type: "nodejs:\{\{version:nodejs:latest}}"
>
> &#x20;           \[...]
>
> &#x20;           relationships:
> &#x20;             database:
>
> &#x20;       services:
> &#x20;         database:
> &#x20;       	  type: oracle-mysql:\{\{version:oracle-mysql:latest}}

## 5. Update `.environment`

When you previously ran `upsun project:init`, the command generated some Strapi-specific environment variables:

```bash .environment theme={null}
# Set Strapi-specific environment variables
export DATABASE_HOST="$DB_HOST"
export DATABASE_PORT="$DB_PORT"
export DATABASE_NAME="$DB_PATH"
export DATABASE_USERNAME="$DB_USERNAME"
export DATABASE_PASSWORD="$DB_PASSWORD"
export DATABASE_SCHEME="$DB_SCHEME"

# Set secrets needed by Strapi, if they are not set
# Prefer setting these as project secret variables with upsun variable:create env:SECRET_NAME --sensitive=true
if [ -z "$ADMIN_JWT_SECRET" ]; then
  export ADMIN_JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
fi
if [ -z "$JWT_SECRET" ]; then
  export JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
fi
if [ -z "$API_TOKEN_SALT" ]; then
    export API_TOKEN_SALT="$PLATFORM_PROJECT_ENTROPY"
fi
if [ -z "$APP_KEYS" ]; then
    export APP_KEYS="$PLATFORM_PROJECT_ENTROPY"
fi
```

Upsun Cloud will actually generate service credentials automatically for you in the runtime container, so we don't need the first half of this file anymore.
Remove the first block (pertaining to `DATABASE` credentials).

Then, add a single additional variable that will set the `DATABASE_CLIENT` variable at the appropriate time:

> [tab] PostgreSQL
>
> ```bash .environment theme={null}
> # Set secrets needed by Strapi, if they are not set
> # Prefer setting these as project secret variables with upsun variable:create env:SECRET_NAME --sensitive=true
> if [ -z "$ADMIN_JWT_SECRET" ]; then
>   export ADMIN_JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$JWT_SECRET" ]; then
>   export JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$API_TOKEN_SALT" ]; then
>     export API_TOKEN_SALT="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$APP_KEYS" ]; then
>     export APP_KEYS="$PLATFORM_PROJECT_ENTROPY"
> fi
>
> # Switch to configure to the production database service _only_ at deploy time.
> if [ -z "$PLATFORM_ENVIRONMENT" ]; then
>     export DATABASE_CLIENT="postgres"
> fi
> ```

> [tab] Oracle MySQL
>
> ```bash .environment theme={null}
> # Set secrets needed by Strapi, if they are not set
> # Prefer setting these as project secret variables with upsun variable:create env:SECRET_NAME --sensitive=true
> if [ -z "$ADMIN_JWT_SECRET" ]; then
>   export ADMIN_JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$JWT_SECRET" ]; then
>   export JWT_SECRET="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$API_TOKEN_SALT" ]; then
>     export API_TOKEN_SALT="$PLATFORM_PROJECT_ENTROPY"
> fi
> if [ -z "$APP_KEYS" ]; then
>     export APP_KEYS="$PLATFORM_PROJECT_ENTROPY"
> fi
>
> # Switch to configure to the production database service _only_ at deploy time.
> if [ -z "$PLATFORM_ENVIRONMENT" ]; then
>     export DATABASE_CLIENT="mysql"
> fi
> ```

## 6. Push to the environment

Commit and push the changes to the Upsun Cloud environment:

```bash theme={null}
git commit -am "Add a new service"
git push origin upgrade-db
```

> [!NOTE]
>
> If you are using Upsun Cloud as your primary remote, you can use the `upsun branch` and `upsun push` commands directly.
> If instead you had already set up an integration to GitHub, GitLab or Bitbucket, make sure to open a pull/merge request to judge the revision.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.