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"
, andexport 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.
FATAL: password authentication failed for user "%= ENV['DB_USERNAME'] %"
. – Rochet