我有一个使用一键式 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
数据库.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
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"}}
最佳答案
事实证明,重要的区别在于 Spring 预加载器。似乎您在定义 ENV 变量之前启动了 Spring。
尝试杀死 Spring 并重新启动控制台。
关于ruby-on-rails - Rails 控制台无法连接到数据库,但 rake 任务可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392016/