Skip to content

Instantly share code, notes, and snippets.

@ldoguin
Last active December 30, 2020 12:02
Show Gist options
  • Save ldoguin/4aaf2feaf8a9a12a9c1c85c7628856e4 to your computer and use it in GitHub Desktop.
Save ldoguin/4aaf2feaf8a9a12a9c1c85c7628856e4 to your computer and use it in GitHub Desktop.
How to manage two postgresql instance for a single application

How to manage two postgresql instance for a single application

If you have an application that requires two PostgreSQL databases, both managed by Clever Cloud, you might have an issue. The naive way to do this would be to link the two PGs addons. But then you would have an issue because the provided environment variables have the same name, so you would only have access to one PG.

Here's a quick and dirty way to manage an additional database automatically.

For the application that needs two databases:

Configuration

Whatever you need

Exposed Configuration

LINKED_APPLICATION_ID app_033368e2-c318-4c9b-bd6c-e53010825e86

Service Dependencies

Link addons -> theFirstDB # link it to the first PostgreSQL instance

For the task application

Configuration

CC_PRE_RUN_HOOK="./interpolator.sh" # execute the script that will add the second DB env variables/
CLEVER_SECRET="XXXXXXXXXXXXXX" # the clever secret to login with clever tools
CLEVER_TOKEN="XXXXXXXXXXXXXXXXXXXXX" #the clever token to login with clever tools
PORT="8080"

Service Dependencies

Link addons -> theSecondDatabase # Link it to the second PostgreSQL instance

Link applications -> theApplicationFor2PGs # Link it to the main app to get its exposed applicationID

The content of the interpolator.sh script.

#!/bin/bash
clever link $LINKED_APPLICATION_ID
# set the variables for the second DB
clever env set POSTGRESQL_ADDON_DB_2 $POSTGRESQL_ADDON_DB
clever env set POSTGRESQL_ADDON_HOST_2 $POSTGRESQL_ADDON_HOST
clever env set POSTGRESQL_ADDON_PASSWORD_2 $POSTGRESQL_ADDON_PASSWORD
clever env set POSTGRESQL_ADDON_PORT_2 $POSTGRESQL_ADDON_PORT
clever env set POSTGRESQL_ADDON_URI_2 $POSTGRESQL_ADDON_URI
clever env set POSTGRESQL_ADDON_USER_2 $POSTGRESQL_ADDON_USER
clever restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment