Rails控制台无法连接到数据库,但rake任务可以

时间:2016-05-23 13:15:02

标签: ruby-on-rails ruby postgresql

我有一个使用单击DO图像的rails应用程序。

我可以成功运行任何rake db:*但是当我运行rails console时,我无法连接到数据库。

我该如何解决?该应用程序运行正常。所以我认为是rails console问题。

控制台输出:

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'] %>

修改

环顾四周,我发现ActiveRecord::Base.configurationspassword: nil。为什么服务器进程会读取密码,但控制台没有?

我将ActiveRecord::Base.configurations添加到初始化程序中。在初始化时,密码存在。

在控制台ActiveRecord::Base.configurations上按预期返回所有信息,密码除外。我甚至用. /etc/defaults/unicorn

重新加载了默认变量

编辑2

ActiveRecord::Base.configurations

上的

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

上的

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

上的

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"}}

1 个答案:

答案 0 :(得分:11)

事实证明,重要的区别是Spring预加载器。似乎您已经 之前启动了Spring ,您已经定义了ENV变量。

尝试杀死Spring并重新启动控制台。