Nicos Karalis
Nicos Karalis

Reputation: 3773

Rails console can't connect to database but rake tasks can

I have a rails app that is using the one click DO image.

I can run any of the rake db:* successfully but when I run rails console I can't connect to the db.

How can I fix it? The app is working fine. So I think is a rails console problem.

Console output:

deployer:/home/rails$ RAILS_ENV=production bundle exec rake db:migrate
  ActiveRecord::SchemaMigration Load (3.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"

deployer:/home/rails$ echo $APP_DATABASE_PASSWORD
[redacted database password]

deployer:/home/rails$ RAILS_ENV=production bundle exec rails console
Running via Spring preloader in process 25038
Loading production environment (Rails 4.2.6)
2.3.0 :001 > User.connection
PG::ConnectionBad: fe_sendauth: no password supplied

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  host: localhost
  username: rails
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

production:
  <<: *default
  database: production
  username: rails
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

EDIT

Looking around I found that ActiveRecord::Base.configurations has password: nil. Why then the server process reads the password but console doesn't?

I added ActiveRecord::Base.configurations to an initializer. At initialization the password is present.

On console ActiveRecord::Base.configurations returns all the info as expected, except by the password. I even have reload the default vars with . /etc/defaults/unicorn

EDIT 2

ActiveRecord::Base.configurations on RAILS_ENV=production bundle exec rails console

{"default"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil},
 "development"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"development"},
 "test"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"test"},
 "production"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"production"}}

ActiveRecord::Base.configurations on APP_DATABASE_PASSWORD=password RAILS_ENV=production bundle exec rails console

{"default"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil},
 "development"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"development"},
 "test"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"test"},
 "production"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>nil,
   "database"=>"production"}}

ActiveRecord::Base.configurations on RAILS_ENV=production bundle exec rails server

{"default"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>[password]},
 "development"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>[password],
   "database"=>"development"},
 "test"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>[password],
   "database"=>"test"},
 "production"=>
  {"adapter"=>"postgresql",
   "encoding"=>"unicode",
   "pool"=>5,
   "host"=>"localhost",
   "username"=>"rails",
   "password"=>[password],
   "database"=>"production"}}

Upvotes: 5

Views: 2572

Answers (1)

Matouš Bor&#225;k
Matouš Bor&#225;k

Reputation: 15954

It turns out that the important difference is the Spring preloader. It seems that you've started Spring before you've defined the ENV variable.

Try killing Spring and restarting the console.

Upvotes: 13

Related Questions