Specify the database.yml file when starting rails server

When working on multiple features at the same time, it pays off to have a separate copy of the DB for each branch in order to avoid schema changes from other branches that don’t make sense when submitting a PR. Most of the time you end up reverting multiple migrations just to get rid these changes in the commit.

One way is to change the database.yml file each time you switch branches, another would be to have a separate yml file that can be selected when restarting the server after switching the branch.

Something like bin/rails s -D database_<feature_x>.yml could be used to specify the yml file, which is not possible in rails at the time.

I believe you can specify DATABASE_URL=postgres://… bin/rails s. Would that work for your use case?

Take a look at the guides article section about connecting to a DB which outlines the load order: Configuring Rails Applications — Ruby on Rails Guides

Would have been a great solution if both DBs were in the localhost, but i’m afraid i have to connect to a remote DB sometimes which requires username and a password as well. I wanted a simpler solution which would require specifying a database.yml that i could use instead of writing a huge DATABASE_URL each time i had to switch DBs. I was actually surprised that rails didn’t support this, as this is the most standard way to select config files for any executable

You could use a shell script with DATABASE_URL saved in it, or (if you’re using 1password or a similar tool) automatically load your DATABASE_URL from 1password using their command line tool.