我有一个使用单击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.configurations
有password: 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"}}
答案 0 :(得分:11)
事实证明,重要的区别是Spring预加载器。似乎您已经 之前启动了Spring ,您已经定义了ENV变量。
尝试杀死Spring并重新启动控制台。