Connecting to an existing database (Supabase)

Hi there :slight_smile:

I’m new to Directus (just setting up), and I didn’t find the answer in the docs nor in the forum - sorry if it has already been answered!

I already have a Supabase (cloud) project, and I would like to connect Directus to this existing database, to offer a simple way to interact with data for non-tech users.

I created a project on Directus Cloud version, but never saw an option to connect an existing database.
I also tried to self-host Directus with Pikapods: never saw this option either.

I’m pretty sure it is possible to do so (https://directus.io/features/existing-database, Add Directus to an Existing Database), but cannot find the proper way to do it.

How am I supposed to be given this option?
Thank you for your help! :slight_smile:

2 Answers

2

Hi Manon

You can only connect an existing database when you self-host Directus, since Directus Cloud manages its own database and doesn’t allow external connections.

If you’re self-hosting, you’ll need to set these environment variables with your Supabase credentials:

DB_CLIENT=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USER=
DB_PASSWORD=
DB_SSL=true

You can find these details in your Supabase → Settings → Database → Connection Info section.

Refer to the official docs for more details:

Once set, start your Directus instance and it should automatically connect to your Supabase database.

Hope that helps!

Thanks a lot, I’ve tried this but I’m not really self-hosting: I’m now managing Directus via Elestio.

In Elestio, I have two ways to access the environment config.

One called ENV that looks like this:

SOFTWARE_VERSION_TAG=v9.26.0
SOFTWARE_PASSWORD=XXXXXXXXXXXXXXXXX (here appears the password)
ADMIN_EMAIL=XXXXXXXXX (here appears the email)
ADMIN_PASSWORD=XXXXXXXXXXXXXXXXX (here appears the password)
DOMAIN=directus-vnifu-u61462.vm.elestio.app
EMAIL_FROM=directus-vnifu-u61462.vm.elestio.app@vm.elestio.app

There, I added the following lines:

DB_CLIENT=pg
DB_HOST=db.eugngjagsvyhwtmigfye.supabase.co
DB_PORT=5432
DB_DATABASE=postgres
DB_USER=postgres
DB_PASSWORD=XXXXXXXXXXXXXXXXX (here the database password)
DB_SSL=true

Unfortunately, when restarting the service with this config, I still have an “empty” Directus, with no collections.

The other one called Docker Compose that looks like this:

version: "3.3"
services:
  database:
    image: elestio/postgres:15
    restart: always
    volumes:
      - ./data:/var/lib/postgresql/data
    networks:
      - directus
    environment:
      POSTGRES_USER: "directus"
      POSTGRES_PASSWORD: ${SOFTWARE_PASSWORD}
      POSTGRES_DB: "directus"
  cache:
    image: redis:6
    restart: always
    networks:
      - directus
  directus:
    restart: always
    image: elestio/directus:${SOFTWARE_VERSION_TAG}
    ports:
      - 172.17.0.1:8055:8055
    user: 0:0
    volumes:
      - ./uploads:/directus/uploads
      - ./extensions:/directus/extensions
    networks:
      - directus
    depends_on:
      - cache
      - database
    environment:
      KEY: ${SOFTWARE_PASSWORD}
      SECRET: ${SOFTWARE_PASSWORD}
      PUBLIC_URL: https://${DOMAIN}
      DB_CLIENT: "pg"
      DB_HOST: "database"
      DB_PORT: "5432"
      DB_DATABASE: "directus"
      DB_USER: "directus"
      DB_PASSWORD: ${SOFTWARE_PASSWORD}
      EMAIL_FROM: ${EMAIL_FROM}
      EMAIL_TRANSPORT: "smtp"
      EMAIL_SMTP_HOST: "172.17.0.1"
      EMAIL_SMTP_PORT: 25
      EMAIL_SMTP_SECURE: "false"
      EMAIL_SMTP_IGNORE_TLS: "true"
      CACHE_ENABLED: "true"
      CACHE_STORE: "redis"
      CACHE_REDIS: "redis://cache:6379"
      REDIS: "redis://cache:6379"
      ADMIN_EMAIL: ${ADMIN_EMAIL}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
      CACHE_AUTO_PURGE: "true"
      NODE_TLS_REJECT_UNAUTHORIZED: 0
networks:
  directus:

There, I tried replacing these lines like this:

DB_CLIENT: "pg"
DB_HOST: "db.eugngjagsvyhwtmigfye.supabase.co"
DB_PORT: "5432"
DB_DATABASE: "postgres"
DB_USER: "postgres"
DB_PASSWORD: "XXXXXXXXXX" (with my database password)

But when I restart the service, I get a connection error (ENETUNREACH).

Would you help me identify how to enter the environment variables correctly?

Hello, try to also add the DB_SSL as true in the docker compose and see if it works.

Thanks, I tried but I get the same error... any other idea?

ENETUNREACH sounds like a networking error like you'd find when the internet connection is down. Maybe Elestio isn't configured to allow traffic out of the network? When using Docker Compose, usually you have to explicitly allow the container to access the host network to be able to reach the internet, so that could definitely be the reason why it's not able to connect to the database. Might be worth trying network mode "host" https://docs.docker.com/reference/compose-file/services/#network_mode