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

标签 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

数据库.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.configurationsRAILS_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/

相关文章:

ruby-on-rails - 产品中的 Rails 5:PG::ConnectionBad:致命:使用 Docker 的用户 "postgres"的密码身份验证失败

ruby-on-rails - 如何根据rails中的当前范围修改datagrid上的过滤器

ruby-on-rails - 如何使 Rails 模型无效

PostgresQL Windows 安装程序 - 空白屏幕 -

java - Postgresql 无法接受 SSL 连接 : sslv3 alert certificate unknown

ruby-on-rails - 检测到 Heroku 不支持的 sqlite3 gem

ruby-on-rails - 解析 JSON 数据时遇到问题

ruby-on-rails - Capybara、Devise、CanCan 和 RSpec 集成测试 : valid sign in 302 redirects to example. com

ruby - 我无法运行 compass watch

sql - 某些代码没有 CREATE FUNCTION 的别名?