Default environment specific values for DATABASE_URL

Apr 11, 2012

If you've been following the intelligent advice from 12 Factor

when building your application then you'll know that environment variables

are the correct place to specify settings for your environment. Crazy eh?

However this is at odds with the assumptions and conventions in Rails that

database.yml is the canonical source of all your database configs. Having

to export your environment variables any time you want to change

the environment of the app, or over-riding in test_helper/spec_helper

is needless pain.

Keeping DATABASE_URL and database.yml consistent

Instead of having to manage all this manually, lets just let Rails win

for now by setting DATABASE_URL to the value(s) defined within database.yml.

No more having to set default values in your helpers, no more re-exporting

your environment locally when you want to change modes, no more exports

if you're using libraries that assume you're configuring your app in an

intelligent and scalable way (like queue_classic).

Add this to your Gemfile:

gem "rails-database-url"

That's it. Source is on GitHub, pull requests

gladly accepted if you find any bugs.

Hi, I'm Glenn! 👋 I'm currently Director of Product @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.