2016-05-10 4 views
1

Meine Rails App verbindet sich mit der Datenbank auf meinem Entwicklungscomputer und in der Produktion gut. Ich habe das Projekt auf den Computer eines Kollegen geklont und die Datenbank lokal von einem pgdump neu erstellt. Derselbe Vorgang funktioniert auf meiner Maschine einwandfrei. Aber auf seinem,Rails/Postgres Verbindungsproblem (psql und pg funktioniert gut)

psql in der Befehlszeile ist, die keine Probleme:

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

ROL2=# 

Und das pg Juwel zu testen (pg-0.18.4), habe ich eine einfache Ruby-Skript-Ausgabe geschrieben unsere Benutzername:

#!/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 

Kein Problem; funktioniert gut. Aber wenn ich über die Schienen-Konsole oder eine Aufgabe in der App versuchen verbinden:

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

Hier sind die database.yml:

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

Ditto, wenn ich versuche, mit einem Client (Postico) zu verbinden. Ich habe es mit dem Postgres-Benutzer und seinem Benutzernamen/Passwort probiert.

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

Warum könnte Rails haben Probleme mit der Datenbank verbinden, obwohl psql und die pg gem arbeiten gut?

Added: der Fehler als erscheint es in den Rails-Entwicklung Protokollen:

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' 
+0

Ihre Entwicklung oder Postgres Logs sagen nichts? – PhilVarg

+0

Die Entwicklerlogs wurden hinzugefügt. Die Fehler werden in den Postgres-Protokollen nicht angezeigt, was sinnvoll ist, wenn keine Verbindung hergestellt werden kann. – hoverbikes

Antwort

2

Da die database.yml Host und Port umfasst, Rails wahrscheinlich versucht, den TCP-Socket, während Ihre Verbindungen über psql zu verbinden und pg (da sie Host oder Port nicht angeben) verwenden den UNIX-Socket. PostgreSQL kommt standardmäßig aus dem Lieferumfang nicht hören Sie den TCP-Socket, so schauen Sie sich die postgresql.conf und stellen Sie sicher listen_addresses unkommentiert. Wenn dies nicht der Fall ist, können Sie versuchen, entweder Host und Port aus dem database.yml herauszunehmen oder die Konfiguration zu ändern, um TCP-Verbindungen zu aktivieren und PostgreSQL neu zu starten.

+0

Diese Antwort hilft Menschen, die die gleichen Symptome haben. Der Unterschied zwischen der Verbindung über den UNIX-Socket (gegenüber TCP) war tatsächlich der Grund, warum psql und pg das Problem durcharbeiteten. (In meinem Fall war Postgres-Konfiguration jedoch nicht das Problem, obwohl mein Kollege eine Drittanbieter-Anwendung installiert hatte, die lokale Verbindungen auf Port 5432 blockierte.) – hoverbikes

+0

Ah, okay. Ich bin froh, dass es dich wenigstens auf die richtige Spur gebracht hat. Danke für die Annahme! –