Rails 4.1: Database URLs

Tuesday, 29 April 2014

DHH mentioned in the Rails 4.1 announcement blog post that it now supports configuration of database connetions through URLs. I couldn’t find any documentation for the feature so here’s how it works.

Your config/database.yml can now go from something like this:

development:
  adapter: postgresql
  database: app_development
  host: localhost

To this:

development: "postgresql://localhost/app_development"

You can also specifiy the database URL as an environment variable when running your app, for example:

DATABASE_URL=postgresql://localhost/app_development bundle exec rails s

Unfortunately you can only override an existing config with an env var if you use the original (non url based) syntax in your database.yml file.

Configuration via env var is very useful in production environments where service discovery is used and saves having to rewriting the database.yml each time just to point at a different database.

blog comments powered by Disqus