Configuring a Database:
You can connect to the database by setting an environment variable
ENV['DATABASE_URL'] or by using a configuration file called
config/database.yml file contains sections for three different environments in which Rails can run by default:
config/database.ymlfile can contain ERB tags
<%= %>. Anything in the tags will be evaluated as Ruby code. You can use this to pull out data from an environment variable or to perform calculations to generate the needed connection information.Since it is possible to embed ERB in your
config/database.ymlit is best practice to explicitly show you are using the
ENV['DATABASE_URL']to connect to your database. This is especially useful in production since you should not commit secrets like your database password into your source control (such as Git).
$ cat config/database.yml
url: <%= ENV['DATABASE_URL'] %>
- If you have an empty
config/database.ymlfile but your
ENV['DATABASE_URL']is present, then Rails will connect to the database via your environment variable.