【问题标题】:PG Error could not connect to server: Connection refused Is the server running on port 5432?PG 错误无法连接到服务器:连接被拒绝服务器是否在端口 5432 上运行?
【发布时间】:2014-08-11 13:38:45
【问题描述】:

我最近更新了我的机器(运行 Mountain Lion 的 MacBook Pro),从那时起,每当我设置一个 rails 项目时,我的机器就会崩溃并且无法连接到 PG。目前,这是我尝试访问 localhost 时遇到的错误;

PG::ERROR 无法连接到服务器:连接被拒绝服务器是否在主机“localhost”上运行
(::1) 并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接
拒绝 服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(fe80::1) 上运行并接受端口 5432 上的 TCP/IP 连接?

这是一个清单,以及我尝试过的事情的清单;

  1. PostgreSQL 版本为 9.3.4
  2. 我通过 HomeBrew 安装了它
  3. 我的 database.yml 文件中有 host: localhost
  4. Rails 版本 4.1.1,ruby 2.0.0p247(2013-06-27 修订版 41674)[x86_64-darwin12.5.0]
  5. brew uninstall postgresql 然后brew install postgresql
  6. 试图访问我的 postgres.conf 文件,但不允许通过 PGAdmin 和命令行访问
  7. 跑了locate pg_hba.conf,终端把它拍了出来;

    WARNING: The locate database (/var/db/locate.database) does not exist.
    
  8. 我也尝试过使用 rails 4.0 但我仍然遇到问题

  9. 我仍然可以在我的计算机上处​​理在我更新计算机之前启动/完成的其他项目。

  10. 当我从终端运行 psql 时,我得到 psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

我真的迷路了,不知道还能做什么。任何帮助将不胜感激。

更新:

感谢this 的问题,只需下载适用于 Mac 的 Postgres.app 即可解决所有问题。现在一切都很好。

【问题讨论】:

  • 你能从控制台连接到你的PG吗? (psql)?
  • 好像您已经验证 Postgres 已安装,但它也需要运行! :)
  • @UriAgassi 我应该补充一点,当我输入 psql 时,我得到了; psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
  • @UriAgassi 我可以通过下载和使用 Mac 版 Postgres.app 来解决这个问题。效果很好:)

标签: ruby macos postgresql ruby-on-rails-4


【解决方案1】:

这比您想象的要容易,因为如果您更新您的 mac,则进程正在运行,但这里的 /usr/local/var/postgres/ 缺少 postmaster.pid 文件。所以你需要为创建 postmaster.pid 的 postgres 启动进程。

如果进度正在运行,那么 -

手动停止:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

手动启动:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

现在一切正常...

【讨论】:

    【解决方案2】:

    在命令行中运行

    postgres -D /usr/local/var/postgres
    

    如果结果和下一个一样

    LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
    FATAL:  database files are incompatible with server
    DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.
    

    运行

    brew postgresql-upgrade-database
    

    基于此post

    【讨论】:

    • 谢谢你。在我安装了一个不相关的包后遇到了同样的问题。 Homebrew 更新的 PG 和 DB 文件变得不兼容。
    • 我的 mac 升级到 Big Sur 时必须这样做
    • 你拯救了我的一天,谢谢!
    【解决方案3】:

    对于 M1 系统,/usr/local/ 文件移动到 /opt。因此,homebrew 在 /opt/homebrew/var/postgres 安装 postgres。

    尝试手动启动服务器,

    pg_ctl -D /opt/homebrew/var/postgres -l /opt/homebrew/var/postgres/log start
    

    然后尝试检查服务器是否正在运行,

    pgrep -l postgres
    

    如果上面的 cmd 输出,服务器运行正常。 尝试查找服务器连接,

    sudo find /tmp/ -name .s.PGSQL.5432
    

    然后通过,连接到db,

    psql -h /tmp/ databaseName
    

    【讨论】:

      【解决方案4】:

      如果你是通过 brew 安装的

      $ brew services stop postgresql
      #adjust below  path accordingly to your machine 
      $ rm /usr/local/var/postgres/postmaster.pid 
      $ brew services start postgresql
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 2015-01-31
        • 2018-09-24
        • 1970-01-01
        • 2013-11-15
        相关资源
        最近更新 更多