PG::ConnectionBad - 无法连接到服务器:连接被拒绝 [英] PG::ConnectionBad - could not connect to server: Connection refused

查看:55
本文介绍了PG::ConnectionBad - 无法连接到服务器:连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行 Rails 4.0 服务器时,我都会得到这个输出.

 2013-11-06 23:56:36 -0500 开始 GET "/" for 127.0.0.1PG::ConnectionBad - 无法连接到服务器:连接被拒绝服务器是否在主机localhost"(::1) 上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接被拒绝服务器是否在主机localhost"(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接被拒绝服务器是否在主机localhost"(fe80::1) 上运行并接受端口 5432 上的 TCP/IP 连接?:activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:542:in`初始化'activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection'activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection'activerecord (4.0.0) lib/active_record/migration.rb:792:in `current_version'activerecord (4.0.0) lib/active_record/migration.rb:800:in `needs_migration?'activerecord (4.0.0) lib/active_record/migration.rb:379:in `check_pending!'activerecord (4.0.0) lib/active_record/migration.rb:366:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'activesupport (4.0.0) lib/active_support/callbacks.rb:373:in`_run__1613334440513032208__call__callbacks'activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'Better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'Better_errors (0.9.0) lib/better_errors/middleware.rb:79:in`better_errors_call'Better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'rack (1.5.2) lib/rack/runtime.rb:17:in `call'activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'机架 (1.5.2) lib/rack/lock.rb:17:in `call'actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'railties (4.0.0) lib/rails/engine.rb:511:in `call'railties (4.0.0) lib/rails/application.rb:97:in `call'rack (1.5.2) lib/rack/content_length.rb:14:in `call'瘦 (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'瘦 (1.5.1) lib/thin/connection.rb:79:in `pre_process'瘦 (1.5.1) lib/thin/connection.rb:54:in `process'瘦 (1.5.1) lib/thin/connection.rb:39:in `receive_data'eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'瘦 (1.5.1) lib/thin/backends/base.rb:63:in `start'瘦 (1.5.1) lib/thin/server.rb:159:in `start'机架 (1.5.2) lib/rack/handler/thin.rb:16:in `run'机架 (1.5.2) lib/rack/server.rb:264:in `start'railties (4.0.0) lib/rails/commands/server.rb:84:in `start'railties (4.0.0) lib/rails/commands.rb:78:in `block in 'railties (4.0.0) lib/rails/commands.rb:73:in `'bin/rails:4:in `<main>'

我运行的是 Mavericks OS X 10.9,所以我不知道这是否是问题所在.我已经尽我所能,但似乎没有任何效果.我已经多次卸载并安装 postgres 和 pg gem.

这是我的database.yml文件

开发:适配器:postgresql编码:unicode数据库:metals-directory_development池:5用户名:密码:模板:模板0主机:本地主机端口:5432测试:&测试适配器:postgresql编码:unicode数据库:metals-directory_test池:5用户名:密码:模板:模板0主机:本地主机端口:5432分期:适配器:postgresql编码:unicode数据库:metals-directory_production池:5用户名:密码:模板:模板0主机:本地主机生产:适配器:postgresql编码:unicode数据库:metals-directory_production池:5用户名:密码:模板:模板0主机:本地主机黄瓜:<<:*测试

解决方案

它可以像陈旧的 PID 文件一样简单.它可能会静默失败,因为您的计算机没有完全完成关机过程,这意味着 postgres 没有删除 PID(进程 ID)文件.

postgres 使用 PID 文件来确保一次只有一个服务器实例正在运行.因此,当它再次启动时,它会失败,因为已经有一个 PID 文件告诉 postgres 服务器的另一个实例已启动(即使它没有运行),它只是没有关闭并删除 PID).

  1. 要修复它,请删除/重命名 PID 文件.找到 postgres 数据目录.在使用自制软件的 macOS 上,它位于 /usr/local/var/postgres/ 中,或 /usr/local/var/log/ 其他系统它可能是 /usr/var/postgres/.在 M1 上,它可能是 /opt/homebrew/var/postgresql.
  2. 要确保这是问题所在,请查看日志文件 (server.log).在最后几行,您将看到:

<块引用>

致命:锁定文件postmaster.pid";已经存在
提示:另一个 postmaster (PID 347) 是否在数据目录/usr/local/var/postgres"中运行?

  1. 如果是这样,rm postmaster.pid

  2. 重启你的服务器.在使用 launchctl(带有自制软件)的 mac 上,以下命令将重新启动服务器.

    brew 服务重启 postgresql

或在旧版本的 Brew

 launchctl 卸载 homebrew.mxcl.postgresql.plistlaunchctl load -w homebrew.mxcl.postgresql.plist

Every time I run my rails 4.0 server, I get this output.

Started GET "/" for 127.0.0.1 at 2013-11-06 23:56:36 -0500

PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
:
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize'
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
 activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
 activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection'
 activerecord (4.0.0) lib/active_record/migration.rb:792:in `current_version'
 activerecord (4.0.0) lib/active_record/migration.rb:800:in `needs_migration?'
 activerecord (4.0.0) lib/active_record/migration.rb:379:in `check_pending!'
 activerecord (4.0.0) lib/active_record/migration.rb:366:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1613334440513032208__call__callbacks'
 activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
 actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
 railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
 railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
 quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
 actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
 rack (1.5.2) lib/rack/runtime.rb:17:in `call'
 activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 rack (1.5.2) lib/rack/lock.rb:17:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
 railties (4.0.0) lib/rails/engine.rb:511:in `call'
 railties (4.0.0) lib/rails/application.rb:97:in `call'
 rack (1.5.2) lib/rack/content_length.rb:14:in `call'
 thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
 thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
 thin (1.5.1) lib/thin/connection.rb:54:in `process'
 thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
 eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
 thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
 thin (1.5.1) lib/thin/server.rb:159:in `start'
 rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
 rack (1.5.2) lib/rack/server.rb:264:in `start'
 railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
 railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
 railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
 bin/rails:4:in `<main>'

I'm running Mavericks OS X 10.9 so I don't know if that's the problem. I've tried everything I could but nothing seems to work. I've uninstalled and install both postgres and the pg gem multiple times now.

This is my database.yml file

development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: 
  password: 
  template: template0
  host: localhost
  port: 5432

test: &test
  adapter: postgresql
  encoding: unicode
  database: metals-directory_test
  pool: 5
  username: 
  password: 
  template: template0
  host: localhost
  port: 5432

staging:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_production
  pool: 5
  username:
  password:
  template: template0
  host: localhost

production:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_production
  pool: 5
  username:
  password:
  template: template0
  host: localhost

cucumber:
  <<: *test

解决方案

It could be as simple as a stale PID file. It could be failing silently because your computer didn't complete the shutdown process completely which means postgres didn't delete the PID (process id) file.

The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).

  1. To fix it remove/rename the PID file. Find the postgres data directory. On macOS using homebrew it is in /usr/local/var/postgres/, or /usr/local/var/log/ other systems it might be /usr/var/postgres/. On M1, it might be /opt/homebrew/var/postgresql.
  2. To make sure this is the problem, look at the log file (server.log). On the last lines you will see:

FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?

  1. If so, rm postmaster.pid

  2. Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.

    brew services restart postgresql
    

OR on older versions of Brew

    launchctl unload homebrew.mxcl.postgresql.plist  
    launchctl load -w homebrew.mxcl.postgresql.plist

这篇关于PG::ConnectionBad - 无法连接到服务器:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆