Conexión rechazada (PGError) (postgresql y Rails)

Sigo recibiendo este error cuando bash ejecutar mi localhost usando “$ rails s”:

(Mac OSX 10.8.3) (ruby 2.0.0p195 (2013-05-14 revisión 40734) [x86_64-darwin12.3.0]) (Rails 3.2.11) (psql (PostgreSQL) 9.2.2) ** instalado con homebrew

He estado haciendo un montón de desinstalación de Postgresql y reinstalación, así que tengo el presentimiento de que puede haber bibliotecas en conflicto en algún lugar … simplemente no sé por dónde empezar.

Tenía Postgresql 9.1 y 9.2 en la misma carpeta y acabo de mover 9.1 a la papelera.

Aquí está la salida cuando ejecuto “rails s” en la terminal

Danny$ rails s ^[b=> Booting Thin => Rails 3.2.11 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server Exiting /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PGError) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5433? could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5433? could not connect to server: Connection refused Is the server running on host "localhost" (fe80::1) and accepting TCP/IP connections on port 5433? from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout' from /Users/Danny/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection' from /Users/Danny/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:308:in `clear_cache!' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/railtie.rb:97:in `block (2 levels) in ' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:418:in `_run__3642716200177086041__prepare__4186317719333288752__callbacks' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/application/finisher.rb:47:in `block in ' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing' from /Users/Danny/Dropbox/programming/coding/f_app/config/environment.rb:7:in `' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require' from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:3:in `block in ' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize' from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:in `new' from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:in `' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:200:in `app' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands/server.rb:46:in `app' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:254:in `start' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands/server.rb:70:in `start' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:55:in `block in ' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:50:in `tap' from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:50:in `' from script/rails:6:in `require' from script/rails:6:in `' 

El mensaje de error es instrumental:

 could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5433? 

Puerto

Puede estar intentando conectarse al puerto incorrecto.
El puerto estándar es 5432 . Verifique cómo (y si lo hizo) inició su servidor de Postgres:

 postgres@db:~$ ps -auxww | grep ^postgres ...  postgres 1274 0.0 0.3 1437240 57308 ? S May27 5:01 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf 

El manual tiene información relacionada aquí.

En mi ejemplo, las configuraciones de /etc/postgresql/9.1/main/postgresql.conf se usaron, lo que dice (entre muchas otras configuraciones):

 port = 5432 

O corre

 netstat -nlp | grep postgres 

O simplemente mira aquí (al menos en Debian o Ubuntu):

 ls -lA /var/run/postgresql/ 

PostgreSQL elige el siguiente puerto libre si crea un nuevo clúster de base de datos. Ya que instaló repetidamente, puede haber confundido los números de puerto.

listen_addresses

O simplemente se olvidó de permitir conexiones TCP / IP . Más información en estas respuestas relacionadas:
– Ejecutar archivo por lotes con comando psql sin contraseña
– ¿Cuál es la diferencia entre los tipos de conexión “local” y “localhost” en pg_hba.conf?
– ninguna entrada pg_hba.conf para el host

En mi caso, el problema se debió a la actualización de postgresql-9.4 a postgresql-9.5 en Ubuntu 16.04. Desde entonces, hubo dos versiones instaladas en algún momento, la última versión instalada, es decir, la configuración de postgresql-9.5 cambió al puerto predeterminado de 5433 (en lugar del estándar predeterminado 5432 ).

El problema se produjo cuando mi proyecto Rails intentó conectarse al puerto predeterminado de postgresql (ya que el puerto no estaba definido explícitamente en el archivo database.yml ) y seguía fallando. Incluso después de la eliminación de postgresql-9.4 más tarde.

Solución cambiando el archivo de configuración de postgresql

La solución está cambiando el puerto en la configuración de postgresql actualizado (9.5). Para hacer esto, abra el archivo /etc/postgresql/9.5/main/postgresql.conf y cambie la línea

 port = 5433 # (change requires restart) 

a

 port = 5432 # (change requires restart) 

y luego reinicie el servidor con sudo service postgresql restart .

Solución con el cambio del archivo ‘database.yml’ de Rails

Alternativamente, puede cambiar el archivo database.yml los Rails mencionando explícitamente el nuevo puerto (es decir, 5433 ) sin cambiar el archivo de configuración de postgresql. Para hacer esto, simplemente agregue una línea como esta

 port: 5433 

y reinicie el servidor de Rails.

 sudo -u postgres pg_ctlcluster 9.3 main stop 

seguido por

 sudo -u postgres pg_ctlcluster 9.3 main restart 

trabajó para mi

En postgresql.conf, /var/lib/pgsql/data/postgresql.conf:
1. cambie los listen_addresses = ‘localhost’ a listen_addresses = ‘*’
2. habilite el puerto predeterminado como 5432, puerto = 5432
También en pg_hba.conf, /var/lib/pgsql/data/pg_hba.conf: intente el método de autenticación como confianza