当前位置: 首页 > 知识库问答 >
问题:

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

龚弘业
2023-03-14

每次运行Rails4.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>'

我正在运行小牛OS X10.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

共有1个答案

倪阳飇
2023-03-14

它可能就像一个过时的PID文件一样简单。它可能会无声无息地失败,因为您的计算机没有完全完成关闭过程,这意味着postgres没有删除PID(进程id)文件。

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

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

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

如果是,则rm postmaster.pid

重新启动服务器。在mac上,使用launchctl(带有homebrew),以下命令将重新启动服务器。

brew services restart postgresql

或在较旧版本的Brew上

    launchctl unload homebrew.mxcl.postgresql.plist  
    launchctl load -w homebrew.mxcl.postgresql.plist
 类似资料:
  • 有很多类似的问题,但都略有不同。 Gemfile: Rails 4.2.6并在返回的命令行中执行: <code>PG::ConnectionBad(无法连接到服务器:没有这样的文件或目录。服务器在本地运行并接受Unix域套接字上的连接“/tmp/.s.PGSQL.5432”? 在启动rails服务器并尝试加载根路径之后,在浏览器中也得到了相同的结果。 我发现在命令行中执行也不起作用,返回:

  • 问题内容: 我正在尝试使用Flask在Raspberry Pi上运行一个简单的Web服务器。当我运行Flask应用程序时,它说: 在http://127.0.0.1:5000/上运行 但是,当我在笔记本电脑的Chrome中输入此地址时, ERR_CONNECTION_REFUSED 我可以在Raspberry Pi的浏览器上打开127.0.0.1:5000。我需要怎么做才能从另一台计算机连接? 问

  • 我正在尝试连接两个docker容器,一个是posgresql,另一个是python flask应用程序。两者都链接正确,python应用程序中的所有连接变量都直接取自postgres容器中通过链接公开的连接变量,并且与检查postgresql容器时发现的连接变量相同。当我将psql与连接字符串中的精确参数一起使用时,即: 成功连接到postgres容器中的数据库,因此我知道postgres正在通过

  • 因此,我的计划是编写一个dockerfile,使用基本映像,然后在其中复制并运行。 SQL脚本:db0.SQL DockerFile:db0.dockerFile null 完成这些操作后,我可以用一个python_team数据库、一个带有示例表的测试模式和一条记录连接到它。 怎么解决?

  • 每次运行Rails应用程序时都会出现此错误(它无法连接到本地Postgresql) 我在用Postgres。它正在正确运行的应用程序。 如果我跑 我可以正确登录到Postgreql控制台。 宝石文件 数据库yml Postgreql(控制台)

  • PSQL:无法连接到服务器:连接被拒绝服务器是否在主机“”上运行并接受端口5432上的TCP/IP连接? Postgres安装版本为9.4。 主机操作系统:Ubuntu 15.04 客户端操作系统:Centos 7 我已经尝试了以下方法,但问题仍未解决: null