My Site

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.

Glenn Gillen

I'm an advisor to, and investor in, early-stage tech startups. Beyond that I'm an incredibly fortunate husband and father. I'm currently a Product Manager at HashiCorp. Previously at AWS and Heroku.

Working on a developer-facing tool or service? Thinking about starting one? Email me and let me know