Default environment specific values for DATABASE_URL

11 April 2012

Default environment specific values for DATABASE_URL

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.

I'm putting together a weekly newsletter to help developers spend more time on what they do best. You should sign up to Cloud Services Weekly  today!