# How to deploy Rails to Fly.io with Supabase DB

* Install `flyctl` command line utility as shown [here](https://fly.io/docs/hands-on/install-flyctl/)
    
* Sign in to Fly.io by running `fly auth login`
    
* Run `flyctl launch` from the rails app directory as shown [here](https://fly.io/docs/hands-on/launch-app/)
    
    * 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:
    
    ```bash
    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
        
        * ```yaml
            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/](https://fly.io/docs/flyctl/secrets/)
        
        * ```bash
            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](https://fly.io/docs/postgres/connecting/app-connection-examples/#connecting-with-rails)
