Automatically point to PR preview database

Hey Oleg,

I am not a render.com staff, just one of their users. So, with a configuration looking something like this:

# render.yaml
previewsEnabled: true # this is important

databases:
  - name: staging
    plan: standard

services:
  - name: api
    type: web
    env: elixir
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: staging
          property: connectionString
  # ...and so on

Your api service will have DATABASE_URL environment variable pointing to a staging DB normally, but for a preview environment (for a pull request), it will point to a PR version of that database created specifically for that PR. It’s done automatically. There’s nothing more you have to do on your side.

So for example in my Elixir application, I just need something like this and that is it.

# config/releases.exs
config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: System.fetch_env!("DATABASE_URL")

Hope this helps.

2 Likes