Upsun User Documentation

Add a database

Sign up

Get your free trial by clicking the link below.

Get your Upsun free trial

Once Your Strapi application has been deployed on Upsun, 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:

  • PostgreSQL
  • Oracle MySQL

1. Branch Anchor to this heading

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

git checkout -b upgrade-db

2. Install the Node.js package Anchor to this heading

yarn add pg
yarn add mysql

3. Add a new service Anchor to this heading

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

.upsun/config.yaml
applications:
    my-strapi-project:
        source:
            root: "/"
        type: "nodejs:20"

        [...]

services:
    database:
	    type: postgresql:16
.upsun/config.yaml
applications:
    my-strapi-project:
        source:
            root: "/"
        type: "nodejs:20"

        [...]

services:
    database:
	    type: oracle-mysql:8.0

4. Add a new relationship Anchor to this heading

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

.upsun/config.yaml
applications:
    my-strapi-project:
        source:
            root: "/"
        type: "nodejs:20"

        [...]

        relationships:
            database: "database:postgresql"

services:
    database:
	    type: postgresql:16
.upsun/config.yaml
applications:
    my-strapi-project:
        source:
            root: "/"
        type: "nodejs:20"

        [...]

        relationships:
            database: "database:mysql"

services:
    database:
	    type: oracle-mysql:8.0

5. Update .environment Anchor to this heading

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

.environment
# 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 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:

.environment
# 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
.environment
# 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 Anchor to this heading

Commit and push the changes to the Upsun environment:

git commit -am "Add a new service"
git push origin upgrade-db

Is this page helpful?