'Not reading ENV variables in database.yml (Rails 4.2.0, RubyMine 7, Postgres, Ruby 2.2.0, DotEnv)

I'm trying to set up a simple rails app (4.2.0, ruby 2.2.0) with PostgreSQL (9.3) using RubyMine (7.0.4); I'm planning on deploying to Heroku.

I'm having problems with two things:

  • First (and more important), my ENV variables aren't working in my database.yml file.

  • Second, RubyMine isn't recognizing erb in that file at all.

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

production:
  <<: *default
#  database: my_app_production
#  username: <%= ENV['DB_USERNAME'] %>
#  password: <%= ENV['DB_PASSWORD'] %>
  url: <%= ENV['DATABASE_URL'] %>

database.env

(I'm using the dotenv-rails gem):

DB_USERNAME=my_app
DB_PASSWORD=password

Edit: I also tried

  • export DB_USERNAME=myapp,
  • DB_USERNAME="my_app", and
  • export DB_USERNAME="myapp"

Gemfile

...
group :development, :test do
  ...
  # Shim to load environment variables from .env into ENV in development.
  gem 'dotenv-rails'
end

I get an error in RubyMine that says I can't connect to my database because

The specified username and password combination is rejected: FATAL: password authentication failed for user "%=...

In the terminal, I get PG::ConnectionBad: FATAL: password authentication failed for user "lee" (my local username), though explicitly passing the variables (rake db:create DB_USERNAME=...) works.

I tried putting the username and password directly into the file:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: my_app
  password: password 

Success! Since the issue wasn't my username and password, I decided to try some basic embedded ruby:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: <%= "my_app" %>
  password: <%= "password" %> 

No luck in RubyMine - FATAL: password authentication failed for user "%=.... However, that does work with rake db:create from a terminal.

So, my two questions are:

  • What step am I missing to make the ENV variables work?
  • How can I make the configuration work with RubyMine?

Edit: First part solved

Apparently in Rails 4.2, the load order is different (Dotenv used to be loaded right after class Application < Rails::Application in application.rb, as per the documentation).

The issue was solved by putting two lines in my application.rb file to load Dotenv earlier:

...
Bundler.require(*Rails.groups)

##### START ADDED CODE #####

Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']

##### END ADDED CODE #####

module MyApp
  class Application < Rails::Application
  ...

I am still trying to figure out how to make RubyMine recognize embedded ruby in database.yml, though now the app works fine through the command line.



Solution 1:[1]

This could be a loading issue. Put the dotenv gem before the pg gem.

Solution 2:[2]

Heroku requires you to set some environment variables in Heroku dashboard -> settings -> config vars

These include:

  • adapter
  • database
  • username
  • password
  • host
  • port

https://devcenter.heroku.com/articles/rails-database-connection-behavior#configuring-connections-in-rails-4-1

Solution 3:[3]

Do you try this?

In your config/database.yml

host: <%= ENV[DB_HOST] %>
username: <%= ENV[DB_USERNAME] %>
password: <%= ENV[DB_PASSWORD] %>

And in your .env

DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=password

Maybe you are missing the host

Solution 4:[4]

username: <%= ENV.fetch("DB_USERNAME") %> worked for my case

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Nys
Solution 2 Sune Nilausen
Solution 3 Martin M.
Solution 4 Shimaa Marzouk