Correct Postgres database setup for Rails 4.2 application on Heroku

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>

+5
source share
4 answers

In fact, many developers prefer to ignore database.yml in version control and never publish it to a repo. The reason for this is that the databases may differ on different machines, which is the reason for not supporting the general configuration.

I am currently working on a Rails 4.2 project, and Heroku has no problem with the absence of database.yml in general (both with PostgreSQL and MySQL, we tested both). What for? Because DATABASE_URL provides all the information needed to access the database, even the name of the adapter . How? Here is the formula:

 adapter://username: password@hostname :port/database?options 

Locally, I use Postgres with peer-to-peer authentication: the database server accepts the same username that I use in my OS, username closed, password useless. The local machine is accepted when host is not set, although I cannot tell if it is trying to communicate via TCP / IP or Unix sockets, so I am fine without host .

Thus, the configuration that you call "shown in the first manual" is reasonable: it contains a minimum number of settings and makes it easy to create additional environments.

+3
source

Most of your assumptions are correct. The following is a reasonable database.yml configuration file.

 default: &default adapter: postgresql encoding: unicode pool: 5 timeout: 5000 development: <<: *default database: app_name_development test: <<: *default database: app_name_test staging: url: <%= ENV['DATABASE_URL'] %> production: url: <%= ENV['DATABASE_URL'] %> 

Make sure RAILS_ENV is set correctly to Heroku (either staging or production ), or Rails will be development by default.

Locally, the test will select the test environment. By default, the application will start in development mode using the development environment.

+2
source

Heroku does not create the .yml database on rails 4.2, because from this version the rails will detect the presence of this environment variable and use it to configure the connection to the database.

Adding

 production: url: <%= ENV['DATABASE_URL'] %> 

What makes things a little more obvious is for those who may not be aware of this but who will not change their behavior. The configils rails reference contains more information on the interactions between database.yml and DATABASE_URL, if you need it.

+1
source

To connect to ActiveRecord without Rails (e.g. sinatra):

 url = URI.parse(ENV['DATABASE_URL']) ActiveRecord::Base.establish_connection( encoding: 'unicode', pool: 5, timeout: 5000, reconnect: true, adapter: url.scheme, host: url.host, database: url.path.sub(%r{^/}, ''), username: url.user, password: url.password ) 
0
source

Source: https://habr.com/ru/post/1213354/


All Articles