You can configure the database in Rails via both the config/database.yml
file and the DATABASE_URL
env var. They are merged but settings from DATABASE_URL
take precedence.
A good solution is to check in the config/database.yml
with a basic configuration thats just enough to support postgres.app
or whatever is the most common solution.
Developers that need other settings can set the DATABASE_URL
env var to customize the database configuration. Gems like figaro and dotenv make this easier.
If you have ever wondered this how Heroku manages to use the correct DB no matter what you throw into database.yml
and the DATABASE_URL
ENV var is how you should be configuring your production DB.
Why should I use ENV vars and not more database_*.yaml files?
The twelve-factor app stores config in environment variables (often
shortened to env vars or env). Env vars are easy to change between
deploys without changing any code; unlike config files, there is
little chance of them being checked into the code repo accidentally;
and unlike custom config files, or other config mechanisms such as
Java System Properties, they are a language- and OS-agnostic standard.
https://12factor.net/config