I am confused about how to set up a database in a Rails 4.2 application that uses Postgres and Heroku.
Following the tips in this Heroku guide , you will get config/database.yml as follows:
default: &default adapter: postgresql encoding: unicode pool: 5 timeout: 5000 development: <<: *default database: app_name_development test: <<: *default database: app_name_test production: <<: *default database: app_name_production
But when I tried this, my development and testing environment used the same database as the intermediate environment (note that the file does not have a configuration for setting). This is not good.
This Ruby database Heroku guide mentions that any Rails applications prior to 4.2 would have a database.yml file rewritten by Heroku. Heroku will DATABASE_URL environment variable and create a new database.yml file.
Therefore, I assume that you can simply leave the configuration in database.yml for any environments that you have on Heroku, for example, staging and production. Your database.yml file may essentially look like Hound's (note the lack of production configuration).
development: &default adapter: postgresql encoding: unicode database: app_development pool: 5 test: <<: *default database: app_test
But since we are using Rails 4.2, I do not think Heroku will override the database.yml file. In this case, you need to specify the database configuration in database.yml for our Heroku environments? Or is it still safe to leave them? If we need to specify a configuration for Heroku environments, will it suffice:
staging: url: <%= ENV['DATABASE_URL'] %> production: url: <%= ENV['DATABASE_URL'] %>
I am also confused about the correct configuration of the development and testing environment. As I mentioned above, the first configuration indicated has these environments using an intermediate database on Heroku, not a local database.
This Heroku tutorial says to export the DATABASE_URL environment variable to connect your application (once Postgres is installed and you can connect to it).
Assuming you export DATABASE_URL env var as stated in the article, what should be your configuration for development and testing? Do we go to the configuration as shown in the first manual, for example
default: &default adapter: postgresql encoding: unicode pool: 5 timeout: 5000 development: <<: *default database: app_name_development test: <<: *default database: app_name_test
Or we use the configuration shown in this Heroku guide (which uses host and username )
development: adapter: postgresql host: localhost username: user database: app-dev
Update 1: This is what I now know. Production and production configuration is not required in config/database.yml if you are deploying to Heroku, regardless of your version of Rails. Prior to 4.2, Heroku created its own database.yml file based on the value of the DATABASE_URL environment variable, overwriting your configuration file (if one exists). Starting with Rails 4.2, your application will use the DATABASE_URL environment variable directly (bypassing the database.yml file), so Heroku will not (and will not) generate a configuration file.
I also found out why my development and testing environments used the remote staging database from the Heroku application instead of the local databases specified in their database.yml configuration. This is because my local .env file for development is based on my intermediate .env file, which contains environment variables for connecting to the database, for example DATABASE_URL . Since DATABASE_URL present in my .env development .env , my Rails 4.2 application used it and thus connected to the staging database. To fix this, I deleted these environment variables from the .env development .env and created local databases (and .env ) using bundle exec rake db:setup .
Update 2: This section of the Rails manual describes in detail how to configure the database, it is worth reading: <a5>