简体   繁体   English

Rails / Postgres 连接问题(psql 和 pg 工作正常)

[英]Rails / Postgres connection issue (psql and pg working fine)

My rails app connects to the database on my development machine, and in production just fine.我的 rails 应用程序连接到我的开发机器上的数据库,并且在生产中很好。 I've cloned the project to a coworker's machine, and recreated the database locally from a pgdump.我已经将项目克隆到同事的机器上,并从 pgdump 在本地重新创建了数据库。 The same process works fine on my machine.同样的过程在我的机器上运行良好。 But on his,但在他身上,

psql at the command line is having no trouble:命令行中的psql没有问题:

$ psql ROL2
psql (9.5.2)
Type "help" for help.

ROL2=#

And to test the pg gem (pg-0.18.4), I've written a simple ruby script to output our users' names:为了测试 pg gem (pg-0.18.4),我编写了一个简单的 ruby​​ 脚本来输出我们的用户名:

#!/usr/bin/env ruby

require 'pg'
conn = PG.connect( dbname: 'ROL2' )
conn.exec( "SELECT * FROM users" ) do |result|
  result.each do |row|
    puts "#{row['first_name']} #{row['last_name']}"
  end
end

No problem;没问题; works fine.工作正常。 But when I try connecting via the rails console or a task in the app:但是当我尝试通过 rails 控制台或应用程序中的任务进行连接时:

$ rails db 
  psql: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

Here's the database.yml :这是database.yml

# LOCAL POSTGRES
development:
  adapter:  postgresql
  encoding: utf8
  host:     localhost
  port:     5432
  database: ROL2
  pool:     5

Ditto when I try to connect with a client (Postico).当我尝试与客户端 (Postico) 连接时也是如此。 I've tried with both the postgres user and his username / password.我已经尝试过 postgres 用户和他的用户名/密码。

ROL2=# \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 joeschmo    | Superuser, Create role, Create DB                          | {}
 postgres    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Why might Rails have trouble connecting to the database, even though psql and the pg gem are working fine?为什么即使 psql 和 pg gem 工作正常,Rails 连接到数据库也有问题?

Added : the error as it appears in the Rails development logs:添加:Rails 开发日志中出现的错误:

PG::ConnectionBad (server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
):
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `initialize'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `new'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `connect'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/Users/mac_osx_username/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `new'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `connect'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
activerecord (4.2.4) lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/Users/mac_osx_username/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/Users/mac_osx_username/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
activerecord (4.2.4) lib/active_record/connection_handling.rb:113:in `retrieve_connection'
activerecord (4.2.4) lib/active_record/connection_handling.rb:87:in `connection'
activerecord (4.2.4) lib/active_record/migration.rb:383:in `connection'
activerecord (4.2.4) lib/active_record/migration.rb:370:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'

Since the database.yml includes host and port, Rails is probably trying to connect to the TCP socket, whereas your connections via psql and pg (since they don't specify host or port) are using the UNIX socket.由于database.yml包括主机和端口,Rails 可能正在尝试连接到 TCP 套接字,而通过psqlpg连接(因为它们没有指定主机或端口)正在使用 UNIX 套接字。 PostgreSQL comes configured out of the box to not listen to the TCP socket, so look at the postgresql.conf and ensure it has listen_addresses uncommented. PostgreSQL 开箱即用配置为侦听 TCP 套接字,因此请查看postgresql.conf并确保它已取消注释listen_addresses If it doesn't, you can try either taking host and port out of the database.yml or modifying the config to enable TCP connections and restarting PostgreSQL.如果没有,您可以尝试从database.yml取出主机和端口,或者修改配置以启用 TCP 连接并重新启动 PostgreSQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM