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个回答

7

定位你的postgres文件,它可能在/usr/local/var/postgres//usr/var/postgres/中,然后删除该文件夹中存在的postmaster.pid文件。


7
如上所建议,我刚刚在我的 Mac 上打开了 Postgres App,点击了“打开Psql”按钮,关闭了 psql 窗口,在终端中重新启动了我的rails 服务器,然后它又开始正常工作了,再也没有出现错误。
相信大家都知道象征 PostgreSQL 的这头大象吧:http://postgresapp.com/

5

如果你在执行brew upgrade升级PostgreSQL到新的主版本(例如从9.3.0升级到9.4.0或更高版本)之后遇到了这个问题,请执行以下操作:

@dmitrygusev's fix from https://github.com/Homebrew/homebrew/issues/35240

Following official [Postgresql] migration guide helped:

brew switch postgres 9.3.5    # presuming you already installed 9.4.1
pg_dumpall > outputfile
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres.old
brew switch postgres 9.4.1
initdb -D /usr/local/var/postgres
psql -d postgres -f outputfile

That's all. Check if import went well, then delete backups:

rm outputfile
rm -Rf /usr/local/var/postgres.old
这里的问题在于,升级Postgres的主要版本时,需要重新创建/迁移数据库。可能需要 chown 目录或手动调用 initdb
另外一些提示,如果您没有使用Homebrew,则可能会有所帮助:
手动停止PG服务器: pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop 手动启动PG服务器: pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 参见:如何在不丢失数据的情况下将PostgreSQL从版本9.6升级到版本10.1?

5

host: localhost放入database.yml文件中,并运行以下命令:

rake db:create db:migrate  

4

在通过Homebrew升级Postgres(我从版本12升级到13)后,我遇到了这个问题。

如果你也出现了这种情况,请尝试运行postgres -D /usr/local/var/postgres并检查输出。在我的情况下,它会声明database files are incompatible with server

你可能会收到一个错误消息,指出postgres.old已经存在。

Error: /usr/local/var/postgres.old already exists!

为了解决这个问题,只需删除该文件。
rm -rf /usr/local/var/postgres.old

修复数据库只需要运行以下命令:
brew postgresql-upgrade-database

3

如果删除postmaster.pid没有解决问题,这个方法对我有用:

cd /usr/local/var/
rm -rf postgres/
mkdir postgres
initdb --locale=C -E UTF-8 postgres/
brew services restart postgresql

感谢@spirito_libero在此贴上的解决方案。


这个操作成功了,但我丢失了所有本地数据库。 - Carl

2

今晚我遇到了这个问题,我一直在开发一个Rails应用程序。我的问题简单地归结为我的PostgreSQL服务器没有运行

我去了屏幕顶部(我使用的是Mac),点击了小象图标并点击了“启动”。

结果发现服务器没有开启。

希望这能为某人提供简单的解决方案。


2

我尝试了以上所有答案,但对我没有用。

在我的情况下,当我检查 /usr/local/var/log/postgres.log 的日志时,它没有错误。但我可以看到它正在监听我的本地IPv6地址,即"::1"

在我的 database.yml 中,它是这样的

host:     <%= ENV['POSTGRESQL_ADDON_HOST'] || '127.0.0.1' %>

我通过改变它进行了修改。
host:     <%= ENV['POSTGRESQL_ADDON_HOST'] || 'localhost' %>

然后它就可以工作了


1
我做了相反的事情,从localhost改为127.0.0.1,然后就可以工作了。 - Ramses

2
使用Postgres应用程序的Mac用户可能想要打开应用程序(在Spotlight中搜索Postgres或在菜单栏中找到大象图标)。在其中,您可能会看到一个带有消息“过时的postmaster.pid文件”的红色X。不幸的是,聚光灯搜索不会显示此文件的位置。点击“服务器设置...”,在打开的对话框框中,单击“显示”按钮以打开数据目录。向内导航一个文件夹(对我来说是“var-10”),然后删除 postmaster.pid 文件。
返回到Postgres应用程序并单击“启动”按钮。那个红色X应该变成一个绿色的勾号,并显示消息“正在运行”。现在,您应该能够成功地在终端中运行Rails命令,例如 rails server Postgres App Server Settings - Show Data Directory

这对我解决了问题。非常感谢您的解决方案!这是一个过程! - boyd

1
我的问题出在application.yml文件中。我的heroku数据库url没有使用5342端口。检查你的heroku配置var DATABASE_URL。确保它与适用数据库中的application.yml完全匹配。

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