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

4

13

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.

Rochet answered 10/3, 2015 at 0:0 Comment(3)
RubyMine tries to connect to the database and failing it gives an error is it?Simarouba
I'm not sure I understand your question, but the error I'm getting is: FATAL: password authentication failed for user "%= ENV['DB_USERNAME'] %".Rochet
There are 2 options to add environment configuration parameters. 1. Add .env file and add your required keys in that .env file. or 2. Add application.yml using Figaro gem to add and whitelist environment configuration parameters.Madea
D
1

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

Drumlin answered 6/4, 2022 at 21:0 Comment(0)
H
0

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

Hargis answered 13/8, 2016 at 0:20 Comment(1)
This was a RubyMine issue, not a ruby one. rails server, etc. could read the embedded ruby no problem; it was just the RubyMine database tool that wasn't executing it. I don't use RubyMine anymore though, so I'm not sure if it's still an issue.Rochet
V
0

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

Veliz answered 29/5, 2018 at 21:41 Comment(0)
U
-1

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

Uncommercial answered 3/7, 2019 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.