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

Posted

技术标签:

【中文标题】PG::ConnectionBad - 无法连接到服务器:连接被拒绝【英文标题】:PG::ConnectionBad - could not connect to server: Connection refused 【发布时间】:2013-11-18 15:39:31 【问题描述】:

每次我运行我的 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

【问题讨论】:

尝试在你的database.yml中添加'host: localhost',从***.com/a/10793186/919641得到这个 @trajan 我已经这样做了。当我第一次开始研究解决方案时,我什至使用了该线程。我已经发布了我的 database.yml 文件。你可以看看它。也许它可能会有所帮助。 显示你的 pg_hba.conf 如果您正在使用 postgres 应用程序,请不要忘记打开它。 感谢您提醒我打开应用程序@PavanKatepalli!好电话;) 【参考方案1】:

您的系统中是否安装了 postgresql?如果没有,请观看Install 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

【讨论】:

我已经安装了它,但是位置在 /usr/local/bin/psql 并且当我运行 sudo -u postgres 时它说 sudo: unknown user: postgres psql 如果你想在你的应用程序中使用 pg 应该可以工作。尝试输入终端 psql metal-directory_development。有什么结果吗?如果没有,那么尝试重新安装 postgres try this for example 当我运行 psql metal-directory_development 我得到 psql: could not connect to server: No such file or directory 服务器是否在本地运行并接受 Unix 域套接字上的连接 "/tmp/.s.PGSQL .5432"?我是一名初学者 Rails 程序员,我不知道这意味着什么。我已经尝试过无数次重新安装 postgres,但都没有成功。【参考方案2】:

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

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

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

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

    如果是,rm postmaster.pid

    重启你的服务器。在使用 launchctl(使用 homebrew)的 Mac 上,以下命令将重新启动服务器。

    brew services restart postgresql
    

或在旧版本的 Brew

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

【讨论】:

#2 是一个很好的建议:“检查 server.log”。我的错误实际上是升级到优胜美地引起的,服务器日志说:FATAL: could not open directory "pg_tblspc": No such file or directory。这个答案帮助我解决了这个问题***.com/questions/25970132/… 我只是赞成这个,因为它给了我足够的解决问题焦土风格。我卸载了 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.plistARCHFLAGS="-arch x86_64" gem install pg。我不知道在让它起作用之前我尝试了多少其他解决方案。 postmaster.opts文件和postmaster.pid文件一样吗? 对于新的 Brew 用户,#4 的命令是 brew services restart postgresql 对我来说(MAC,自制)日志文件是/usr/local/var/log/postgres.log【参考方案3】:

检查文件 postgresql.confubuntu/etc/postgresql/XX/main/postgresql.conf 中)并查找显示的行:

listen_addresses="localhost"

尝试将其更改为:

listen_addresses="*"

它将接受每个 IP,然后检查以下行:

port=5432

并检查是否是您的 database.yml 的同一端口,默认情况下,在我的 postgresql-9.2 上使用 5433 而不是 5432,不要忘记 重启 postgres服务器,

祝你好运!

【讨论】:

这也是我的问题。我怀疑它的发生是因为当我尝试“升级”(即安装 9.5)时,我已经运行了 9.3。我猜它是为了礼貌,将自己配置为在不同的端口上并行运行,而不是接管现有的安装/进程。【参考方案4】:

我遇到了同样的问题,这个解释为我解决了:http://blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/

关键步骤是查看我的 /usr/local/var/postgres/server.log 的尾部, 那让我看看真正的问题是什么,我还没有完全完成升级PostgreSQL的过程

【讨论】:

这里的情况类似——对我来说,关键也是看server.log。在这里找到我的答案:***.com/questions/25970132/…【参考方案5】:

@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

【讨论】:

【参考方案6】:

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

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

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

我找到了here(pjammer 的答案在底部)

【讨论】:

【参考方案7】:

要使用 Postgres 解决这些类型的问题并在 Mac OSX 上使用 Postgres,这可能是 BEST 以及我目前发现的 EASIEST 解决方案:

http://postgresapp.com/

只需下载、安装并开心就好:)

【讨论】:

【参考方案8】:

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

rake db:create db:migrate  

【讨论】:

【参考方案9】:

如上所述,我刚刚在我的 Mac 上打开了 Postgres 应用程序,点击了打开 Psql,关闭了 psql 窗口,重新启动了我的 rails 服务器我的终端,它再次工作,没有更多的错误。

相信大象:http://postgresapp.com/

【讨论】:

【参考方案10】:

如果您在执行 brew upgrade 将 postgres 升级到新的主要版本(f.ex 9.3.09.4.0 或更高版本)后遇到此问题,请执行以下操作:

@dmitrygusev 来自 https://github.com/Homebrew/homebrew/issues/35240 的修复

遵循官方 [Postgresql] 迁移指南有所帮助:

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

就是这样。检查导入是否顺利,然后删除备份:

rm outputfile
rm -Rf /usr/local/var/postgres.old

这里的问题是,在 postgres 的主要版本升级中,有必要重新创建/迁移您的数据库。可能还有chown 目录或手动调用initdb

另见:How to upgrade PostgreSQL from version 9.6 to version 10.1 without losing data?


如果您不使用 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

【讨论】:

【参考方案11】:

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

【讨论】:

【参考方案12】:

我只是运行这个命令sudo service postgresql restart 一切都恢复正常了。

【讨论】:

【参考方案13】:

    卸载 pg:

    gem uninstall pg

    卸载 postgres:

    brew uninstall postgres

    对 postgres 文件夹进行核对,该文件夹中可能存在一堆陈旧的东西:

    rm -rf /usr/local/var/postgres

    重启(可能没必要)

    重新安装pg:

    brew install postgres

    我在 Chris Slade 的回答中的评论以艰难的方式开始 pg,现在我使用 brew 服务在很多方面简化了我的生活:

    brew install services

    并以此开始 pg:

    brew services start postgresql

    重新安装 gem:

    gem install pg

还有你的叔叔。

【讨论】:

小心这种方法......这是一种焦土方法,很可能会破坏您当前拥有的任何本地数据库。看看github.com/copiousfreetime/launchy,你可能可以在lunchy stop postgres 后面跟着lunchy start postgres 在 ubuntu 上我相信 sudo apt purge postgresql 或任何其他 postgresql 包应该足够了,它会破坏一切。 我这样做了,现在得到以下错误:postgresql_adapter.rb:709:in `rescue in connect': FATAL: database "project3_development" 不存在 (ActiveRecord::NoDatabaseError)。我该怎么办 ?我实际上可以在 ruby​​mine 中看到数据库。【参考方案14】:

您需要重新启动 Postgresql 服务器

如果你使用的是ubuntu,你可以通过以下命令重启Postgresql

sudo service postgresql restart

【讨论】:

谢谢你1000次。我刚刚升级了我的 ubuntu 版本,这对我来说是固定的。 虽然问题确实涉及 OSX Mavericks,但当迁移或服务器启动过程中发生奇怪的事情时,此解决方案对于 Ubuntu 安装非常方便。【参考方案15】:

我在生产中遇到了同样的问题(开发一切正常), 在我的情况下,数据库服务器与应用程序在同一台机器上,所以最后起作用的只是通过写入进行迁移:

bundle exec rake db:migrate RAILS_ENV=production

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

【讨论】:

【参考方案16】:

我知道这已经晚了,但可能会对某人有所帮助。 我遇到了同样的问题。原来我有两个版本的 postgres 9.1 和 9.5。我卸载了 9.1 和 9.5,然后再次安装了 9.5,它对我有用。

【讨论】:

【参考方案17】:

找到您的 postgres 文件,它可能位于 /usr/local/var/postgres//usr/var/postgres/ 中,然后删除该文件夹中的 postmaster.pid 文件。

【讨论】:

【参考方案18】:

我停止了 rails 服务器,运行 rake db:migrate 并启动了我的 rails s

【讨论】:

【参考方案19】:

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

在 PostgreSQL 版本 9.6 发布后运行 brew upgrade postgresql 后出现此错误。问题是主要/次要版本升级需要额外的步骤才能将旧日期迁移到新版本。

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

您可以通过检查与 homebrew 一起安装的最新 brew formula 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

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

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

第 7 步。 再次启动 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 uninstall pg
$ gem install pg

第 9 步。(可选)在您确信一切正常后,您可以使用以下命令运行重新获得一些磁盘空间:

brew cleanup postgresql

...如果您真的很勇敢,可以使用以下命令删除旧的 PostgreSQL 数据目录

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

(此答案基于一篇出色的博文 https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/ 并添加了一些内容)

【讨论】:

【参考方案20】:

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

【讨论】:

【参考方案21】:

我在关注更新了 postgresql 的brew upgrade 后遇到了这个错误。我从这篇很棒的帖子中确切地找到了如何解决我的问题。我能够让 postgres 备份并运行,甚至迁移到我所有现有的数据库。 https://coderwall.com/p/ti4amw/how-to-launch-postgresql-after-upgrade

【讨论】:

【参考方案22】:

我刚遇到这个问题,建议的解决方案都不适合我。经过大量的谷歌搜索,我确实找到了解决方案。这对我有用。

首先,我必须运行这个命令来启动服务器,我猜是设置配置文件的位置。

pg_ctl -D /usr/local/var/postgres start && brew services start postgresql

然后我运行这个命令来访问 postgres

psql postgres

然后在 postgres 提示符下,我键入“\du”来列出角色

postgres=# \du

postgres 角色丢失了,所以我必须用这个命令来创建它

CREATE ROLE POSTGRES WITH SUPERUSER CREATEDB CREATEUSER CREATEROLE REPLICATION BYPAs-s-rLS ;

这解决了我的问题,我希望这对其他人有帮助。

【讨论】:

【参考方案23】:

这对我有很大帮助。

$ cd /usr/local/var/postgres/
$ rm postmaster.pid

参考: http://alumni.lewagon.org/questions/60

【讨论】:

【参考方案24】:

使用 Postgres 应用程序的 Mac 用户可能想要打开该应用程序(聚焦搜索 Postgres 或在菜单栏中找到大象图标)。其中您可能会看到带有消息的红色 X:“过时的 postmaster.pid 文件”。不幸的是,聚光灯搜索不会显示此文件的位置。单击“服务器设置...”,在打开的对话框中单击“显示”按钮打开数据目录。导航到一个文件夹(对我来说是“var-10”),然后删除postmaster.pid 文件。

返回 Postgres 应用并单击“开始”按钮。那个红色的 X 应该变成带有“正在运行”消息的绿色复选标记。现在您应该可以在终端中成功运行像rails server 这样的Rails 命令了。

【讨论】:

这为我解决了。非常感谢您的解决方案!这是一个过程!【参考方案25】:

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

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

然后重启 postgres 服务器就可以了!

以下是实现这一目标的步骤:

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

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

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

    kill PID杀死进程,例如,如果我的PID是381,我会用kill 381 重新启动 Postres - 如果使用 brew,请执行 brew services start postgresql。 或者如果使用 postgresapp,只需单击 start 按钮

【讨论】:

【参考方案26】:

在 Osx Movaje 上更新我的 mac 后,我遇到了同样的问题。

我找到了这个解决方案:

首先在终端中尝试下面的命令行:

brew services restart postgresql

如果没有任何改变:

ps aux | grep postgres

如果仍然没有任何变化:

ls -ls | grep post

最后一个修复它的命令,通过从 root 执行删除了 postgres 锁定文件:

rm /usr/local/var/postgres/postmaster.pid

然后:

brew services restart postgresql

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

希望这会有所帮助:)

问候!!

【讨论】:

【参考方案27】:

我今晚刚刚遇到了这个问题,我正在研究一个我已经研究了一段时间的 Rails 应用程序。我的问题归结为 我的 postgresql 服务器没有运行

我转到屏幕顶部(我在 Mac 上)并单击小象图标并单击“开始”。

打开我们的服务器没有打开。

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

【讨论】:

【参考方案28】:

Homebrew 包管理器包括自动启动的launchctl plist。欲了解更多信息,请运行brew info postgres

手动启动:

pg_ctl -D /usr/local/var/postgres start

手动停止:

pg_ctl -D /usr/local/var/postgres stop

自动启动:

"现在启动 postgresql 并在登录时重新启动:"

brew services start postgresql

【讨论】:

【参考方案29】:

我不得不重新安装我的 postgres,这里列出了很好的说明: https://medium.com/@zowoodward/effectively-uninstall-and-reinstall-psql-with-homebrew-on-osx-fabbc45c5d9d

然后我必须创建 postgres 用户: /usr/local/opt/postgres/bin/createuser -s postgres

这种方法会破坏您的所有本地数据,因此请在需要时备份您的数据。

【讨论】:

【参考方案30】:

就我而言,存在权限问题。

当我看到日志时,我发现了问题,运行

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

成功了。

【讨论】:

以上是关于PG::ConnectionBad - 无法连接到服务器:连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Github 操作工作流 PG::ConnectionBad:无法连接到服务器:运行 bundle exec rake 时没有这样的文件或目录

PG::ConnectionBad: 运行 export DATABASE_URL=postgres://$(whoami) 后无法翻译主机名错误

如何修复:PG::ConnectionBad:fe_sendauth:未提供密码

text PG :: ConnectionBad FATAL:角色“Myname”不存在

PG::ConnectionBad FATAL: 角色“我的名字”不存在

fe_sendauth:Ruby没有提供密码(PG :: ConnectionBad),但在Rails中没问题