PG::ConnectionBad - 无法连接到服务器:连接被拒绝

338

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

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

我正在运行Mavericks OS X 10.9,所以我不知道是否存在问题。我已经尝试了一切我所能想到的,但似乎没有什么作用。我已经多次卸载和安装了postgres和pg gem。

这是我的database.yml文件:

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

3
尝试在你的database.yml文件中添加“host:localhost”,这个建议来自https://dev59.com/umgv5IYBdhLWcg3wMNwU#10793186。 - Surendra Bobba
1
@trajan 我已经做过了。我甚至在最开始寻找解决方案时使用了那个线程。我已经发布了我的database.yml文件。你可以看一下。也许它会有所帮助。 - fadelakin
显示你的 pg_hba.conf。 - Pronix
2
如果你在使用Postgres,请别忘了打开该应用程序。 - Pavan Katepalli
1
谢谢提醒我打开应用程序,@PavanKatepalli!好主意 ;) - catch22
显示剩余9条评论
36个回答

1
我只是运行了这个命令 sudo service postgresql restart,然后一切都正常了。

1
我遇到了同样的问题。检查/var/log/postgresqlPostgreSQL日志文件的最后一行。在/etc/postgresql/9.5/main/postgresql.conf文件中有一个未被识别的配置参数。将postgresql.conf中的错误行注释掉解决了我的问题。

1
Homebrew软件包管理器包含自动启动的launchctl plists。了解更多信息,请运行brew info postgres
手动启动: pg_ctl -D /usr/local/var/postgres start 手动停止: pg_ctl -D /usr/local/var/postgres stop 自动启动:
“启动postgresql并在登录时重新启动launchd:” brew services start postgresql

1

类似的情况也发生在我这里——对我来说关键是查看server.log。我在这里找到了答案:https://dev59.com/E18e5IYBdhLWcg3wNYQn - Nathan Wallace

1

我在生产环境中遇到了相同的问题(在开发环境中一切正常), 在我的情况下,数据库服务器与应用程序不在同一台机器上,所以最终解决方法是写入迁移命令:

bundle exec rake db:migrate RAILS_ENV=production

然后重新启动服务器,一切都正常工作。


1

我知道这有点晚了,但可能对某些人有帮助。我遇到了同样的问题。结果发现我有两个版本的postgres 9.1和9.5。我卸载了9.1和9.5,重新安装了9.5,然后问题就解决了。


1

Chris Slade的答案对我有所帮助。

我写了一个小脚本,以便在需要时杀死这些剩余的进程:

kill_postgres() {
  if [[ $* -eq "" ]]; then
    echo "Usage: 'kill_postgres <db_name>' to kill remaining instances (Eg. 'kill_postgres my_app_development')"
  else
    gksudo echo "Granted sudo"
    pids="$(ps xa | grep postgres | grep $* | awk '{print $1}' | xargs)"
    if [[ $pids -eq "" ]]; then
      echo "Nothing to kill"
    else
      for pid in "${pids[@]}"
      do
        echo "Killing ${pid}"
        sudo kill $pid
        echo "Killed ${pid}"
      done
      kill_postgres $*
    fi
  fi
}

1
在我的情况下,存在权限问题。
当我查看日志时,发现了这个问题,运行。
cat /usr/local/var/log/postgres.log 

我发现了。
2020-07-17 15:08:47.495 PKT [16282] FATAL:  data directory "/usr/local/var/postgres" has invalid permissions
2020-07-17 15:08:47.495 PKT [16282] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

我刚刚运行了

sudo chmod -R 700 /usr/local/var/postgres

它起作用了。


0

对我来说,这很简单:我有一个过期的postmaster.pid文件。

只需删除并重新启动postgres即可解决问题:

rm '/Users/<username>/Library/Application Support/Postgres/var-10/postmaster.pid'

0

您不必删除postmaster.pid文件,因为这可能会导致数据损坏。

选项?只需使用kill命令终止进程(不要使用kill -9,普通的kill命令就可以了)。

然后重新启动Postgres服务器,一切都会恢复正常!

以下是实现此操作的步骤:

  1. 找到并打开postmaster.pid文件(我的在Mac Sierra上)

    vi ~/Library/Application\ Support/Postgres/var-10/postmaster.pid

  2. 复制PID - 它是postmaster.pid文件第一行上的数字

  3. 使用kill PID命令终止进程,例如,如果我的PID是381,则执行kill 381
  4. 重新启动Postres - 如果使用brew,请执行brew services start postgresql。 或者如果使用postgresapp,请单击start按钮即可

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接