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

760

问题可能很简单,只是一个陈旧的PID文件。这可能是因为您的计算机没有完全完成关闭过程,这意味着postgres没有删除PID(进程ID)文件而导致默默失败。

PID文件被postgres用于确保每次只运行一个服务器实例。所以当它再次启动时,它会失败,因为已经有一个PID文件,告诉postgres另一个服务器实例已启动(即使它没有运行,它只是没有关闭并删除PID)

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

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

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

  2. 重新启动您的服务器。在使用homebrew的mac上,以下命令将重新启动服务器。

    brew services restart postgresql
    

或者在较旧版本的Brew上使用

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

25
#2 是一个很好的建议:“检查 server.log”。我的错误实际上是由于升级到 Yosemite 导致的,服务器日志显示:FATAL: could not open directory "pg_tblspc": No such file or directory。这个答案帮助我解决了这个问题 https://dev59.com/E18e5IYBdhLWcg3wNYQn - Paul Kaplan
23
我刚刚点赞了这个内容,因为它给了我足够的信息,以焦土政策的方式解决了问题。我卸载了pg(gem uninstall pg),卸载了postgres(brew uninstall postgres),然后清除了闲置的postgres文件夹,其中有一堆陈旧的东西在里面(rm -rf /usr/local/var/postgres)。重启后,执行以下步骤:brew install postgresln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgentslaunchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist,和 ARCHFLAGS="-arch x86_64" gem install pg。在实现这个方法之前,我不知道尝试了多少其他的解决方案。 - IAmNaN
2
邮件管理员.opts文件和邮件管理员.pid文件是相同的吗? - ARTLoe
32
对于新的Brew用户,第4步的命令是 brew services restart postgresql - streetlogics
13
对于我(MAC,homebrew),日志文件路径为/usr/local/var/log/postgres.log - DZack
显示剩余14条评论

57

您需要重新启动Postgresql服务器

如果您使用的是Ubuntu,则可以通过以下命令重新启动Postgresql

sudo service postgresql restart

3
非常感谢您!我刚刚升级了我的Ubuntu版本,这解决了我的问题。 - Mauricio Moraes
虽然这个问题涉及到OSX Mavericks,但是当迁移或服务器启动时出现一些奇怪的情况时,这个解决方案对Ubuntu安装非常有用。 - Jerome

33

我已经成功解决了这个问题,按照Chris Slade的回答操作。但是为了重新启动服务器,我必须使用以下命令:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

我在这里找到了答案(pjammer的回答在底部)

的意思是:

我在这里找到了答案(pjammer在底部回答)

,请注意保留HTML标记。

28
  1. 卸载pg:

  2. gem uninstall pg
  3. 卸载 Postgres:

  4. brew uninstall postgres
  5. 删除可能残留着一堆旧文件的postgres文件夹:

    rm -rf /usr/local/var/postgres
  6. 重启(可能不必要)

  7. 重新安装pg:

  8. brew install postgres
  9. 我在Chris Slade的回答中评论了pg the hard way,现在我使用brew services,这简化了我的生活方式:

  10. brew install services
  11. 使用它开始pg:

    brew services start postgresql
  12. 重新安装 gem:

    gem install pg

然后就万事大吉了。


5
请小心使用这种方法,这是一种"烧毁土地"的方法,很可能会摧毁您当前拥有的任何本地数据库。请查看 https://github.com/copiousfreetime/launchy,您可以尝试执行 lunchy stop postgres 然后执行 lunchy start postgres - lacostenycoder
在Ubuntu上,我相信sudo apt purge postgresql或任何其他postgresql包都应该足够了,它会摧毁一切。 - Fabrizio Bertoglio
我做了这个操作,现在出现以下错误:postgresql_adapter.rb:709:in `rescue in connect': FATAL: database "project3_development" does not exist (ActiveRecord::NoDatabaseError)。我该怎么办?实际上我可以在Rubymine中看到这个数据库。 - Rene Chan

27

您是否已在系统中安装了postgresql?如果尚未安装,请查看安装postgresql。在成功将postgresql集成到系统中后,您可以在系统终端中输入以下内容:

which psql
#=> /usr/bin/psql

接下来您需要在PostgreSQL中创建用户和数据库,如下所示:

sudo su - postgres
psql

然后您可以在终端中看到以下内容:

postgres=#

在此输入:

CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production  WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;

在执行此操作后,您需要更正您的database.yml文件。可能需要像这样的内容:
development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: yourname
  password: passwordhere   ### password you have specified within psql
  host: localhost
  port: 5432               ### you can configure it in file postgresql.conf

如果您在使用postgresql时遇到问题,检查pg_hba.conf是个好主意。


1
我已经安装了它,但位置在 /usr/local/bin/psql,当我运行 sudo -u postgres 时它会显示 sudo: unknown user: postgres。 - fadelakin
如果你想在应用程序中使用pg,那么psql应该可以工作。尝试在终端输入psql metals-directory_development,有什么结果吗?如果没有,则尝试重新安装postgres [例如尝试这个](https://dev59.com/uGgt5IYBdhLWcg3w8iDe#11688953) - Mihail Davydenkov
当我运行psql metals-directory_development时,我得到了psql: could not connect to server: No such file or directory的错误提示。服务器是否在本地运行并接受Unix域套接字“/tmp/.s.PGSQL.5432”的连接?我是一名初学者Rails程序员,对此毫无头绪。我已经尝试过多次重新安装Postgres,但没有成功。 - fadelakin

22
为了解决与Postgres相关的问题,并在Mac OSX上使用Postgres,这可能是我迄今为止发现的最佳且最简单的解决方案:

http://postgresapp.com/

只需下载、安装即可愉快地使用 :)

16

检查文件postgresql.conf(在ubuntu中位于/etc/postgresql/X.X/main/postgresql.conf),查找包含以下内容的行:

listen_addresses="localhost"

尝试将其更改为:

listen_addresses="*"

这意味着它将接受每个IP地址,接下来检查这一行:

port=5432

检查是否与您的database.yml文件使用的端口相同,我的postgresql-9.2默认使用5433而不是5432,别忘了重新启动postgres服务器,祝您好运!


这也是我的问题。我怀疑这是因为当我尝试“升级”(即安装9.5)时,我已经在运行9.3了。我猜它试图礼貌地配置自己以在不同的端口上并行运行,而不是接管现有的安装/进程。 - Joel Fouse

14

正如 @Magne 所描述的那样,错误 PG::ConnectionBad - could not connect to server: Connection refused 可能会在 PostgreSQL 的 major/minor version 升级(例如 9.5 -> 9.69 -> 10)后出现。

我在发布 PostgreSQL 版本 9.6 后运行了 brew upgrade postgresql,然后遇到了这个错误。问题在于,主/次版本升级需要额外的步骤来将旧数据迁移到新版本。

如何检查这是否是您的问题

您可以通过检查使用 homebrew 安装的最新 brew 公式 PostgreSQL 版本来检查这是否是问题...

$ brew info postgresql

/usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M)
Poured from bottle on 2016-10-14 at 13:33:28
/usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) *
Poured from bottle on 2017-02-06 at 12:41:00

然后将其与当前的PG_VERSION进行比较。

$ cat /usr/local/var/postgres/PG_VERSION
9.5

如果PG_VERSION低于最新的brew公式,并且差异是主/次版本更改,则可能是您的问题。 如何解决(即如何升级数据) 以下说明适用于从9.5升级到9.6。根据自己的升级情况更改版本号。 步骤1。 确保PostgreSQL已关闭:
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql

步骤2。创建一个新的原始数据库:

$ initdb /usr/local/var/postgres9.6 -E utf8

步骤 3. 检查旧版本和新版本的二进制文件:

$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1

请注意,在此示例中,我正在从9.5.4二进制升级到9.6.1二进制。
第4步。使用pg_upgrade实用程序将当前数据迁移到新数据库。
$ pg_upgrade \
  -d /usr/local/var/postgres \
  -D /usr/local/var/postgres9.6 \
  -b /usr/local/Cellar/postgresql/9.5.4/bin/ \
  -B /usr/local/Cellar/postgresql/9.6.1/bin/ \
  -v
  • -d标志指定当前数据目录
  • -D标志指定要创建的新数据目录
  • -b指定旧二进制文件
  • -B指定我们要升级到的新二进制文件

步骤5。将旧数据目录移出路径

$ mv /usr/local/var/postgres /usr/local/var/postgres9.5

第六步。 将新创建的数据目录移动到 PostgreSQL 期望的位置

$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres

第七步。再次启动PostgreSQL

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql

步骤8. 如果您在Rails中使用pg gem,应通过卸载和重新安装gem来重新编译(如果您不使用pg gem,则跳过此步骤)

$ gem uninstall pg
$ gem install pg

第九步(可选) 在您确认一切正常工作后,您可以使用以下命令来回收一些磁盘空间:

brew cleanup postgresql

如果你非常勇敢,可以使用以下命令删除旧的PostgreSQL数据目录:

rm -rf /usr/local/var/postgres9.5/

(这个答案基于一篇优秀的博客文章https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/并添加了一些内容)


13

在我将Mac系统升级到OSX Movaje后,我遇到了相同的问题。

我找到了这个解决方法:

首先在您的终端中尝试以下命令:

brew services restart postgresql
如果什么都没有改变:

ps aux | grep postgres
如果仍然没有改变:
ls -ls | grep post

修复它的最后一个命令,从root执行以下命令以删除Postgres锁文件:

最后一个修复命令是从根目录下执行以下命令来移除Postgres锁文件:

```bash rm /var/run/postgresql/.s.PGSQL.5432.lock ```
rm /usr/local/var/postgres/postmaster.pid

然后:

brew services restart postgresql

来自berziiii:https://github.com/ga-wdi-boston/capstone-project/issues/325

希望这能有所帮助 :)

祝好!!


8

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