How to deploy Rails to Fly.io with Supabase DB

How to deploy Rails to Fly.io with Supabase DB

ยท

2 min read

Table of contents

No heading

No headings in the article.

  • Install flyctl command line utility as shown here

  • Sign in to Fly.io by running fly auth login

  • Run flyctl launch from the rails app directory as shown here

    • Remember to answer No when flyctl launch wizard asks if you want to spin up a Postgres DB as part of the launch process since we plan on using Supabase for that.
  • Your app should be would be up, you can verify that in the Fly.io dashboard.

  • Go to Supabase and spin up a database if you've not already done that. Once that DB is up, go to the Database Section of Project Settings, and get the connection String we'll need that later. It'll look something like this:

      postgres://postgres:YOUR-PASSWORD@db.soemthing.supabase.co:6543/mydbname
    
  • Once you have this we've to make two changes to our current setup.

    • Go to database.yml and make sure the database URL for the production environment points to ENV['DATABASE_URL'] as shown below. We need to do this to make Fly.io take the Supabase DB URL

      •   default: &default
            adapter: postgresql
            encoding: unicode
        
          production:
            <<: *default
            url: <%= ENV["DATABASE_URL"] %>
        
    • Now for the final step, we need to add the Supabase DB Connection string as a fly.io app secret. We do that as shown here https://fly.io/docs/flyctl/secrets/

      •   flyctl secrets set DATABASE_URL=postgres://postgres:YOUR-PASSWORD@db.soemthing.supabase.co:66543/mydbname
        
    • The reason we're setting DATABASE_URL is that Rails Apps automatically connect to the Database specified in DATABASE_URL as mentioned here