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've spent most of my career working with or at startups. I'm currently the Director of Product @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.

Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.