Lee
Lee

Reputation: 696

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:

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

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:


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.

Upvotes: 13

Views: 6271

Answers (4)

Martin M.
Martin M.

Reputation: 523

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

Upvotes: 1

Shimaa Marzouk
Shimaa Marzouk

Reputation: 467

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

Upvotes: -1

Sune Nilausen
Sune Nilausen

Reputation: 134

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

Upvotes: 0

Thomas Nys
Thomas Nys

Reputation: 359

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

Upvotes: 0

Related Questions