【问题标题】:Repairing postgresql after upgrade to OSX Mavericks升级到 OSX Mavericks 后修复 postgresql
【发布时间】:2013-11-03 03:31:59
【问题描述】:

最近升级到 OSX Mavericks 后,我的 Rails 应用程序的数据库连接中断。

当我尝试从数据库中获取时,服务器返回以下错误:

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?

当尝试运行 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"?

我已经尝试了互联网上提供的许多解决方案。这样重新安装 pg gem 并在我的 database.yml 中设置主机:localhost。我的 /usr/local/var/postgres/pg_hba.conf 文件说:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     RyanKing                                trust
#host    replication     RyanKing        127.0.0.1/32            trust
#host    replication     RyanKing        ::1/128                 trust

哪个 psql 返回:/usr/local/bin/psql


这个有什么解决办法吗?一些解决方案建议我需要将我的 $PATH 更改为我以前的 postgres 安装,因为新版本的 postgres 将与 Mavericks 一起添加。我如何找到它的位置?很有可能它是用自制软件安装的,但我不确定。

【问题讨论】:

  • 你是如何在 localhost 上安装 postgresql 的?它似乎没有运行,(netstat -na | grep 5432 检查它是否在监听。但是知道你是如何安装它的将有助于弄清楚如何让它重新启动。
  • netstat -na | grep 5432 不返回任何内容。我不记得我是如何安装它的。可能是自制的。
  • PostgreSQL 应该是 Apple 最近在 Mac OS 上安装的。您可能正在搭载该安装,如果安装操作系统升级破坏了它,这似乎很可能。如果您自己安装,它应该不会损坏。

标签: ruby-on-rails ruby macos postgresql osx-mavericks


【解决方案1】:

最简单的解决方案是使用http://postgresapp.com,它可以正常工作

要通过自制软件安装,请确保执行brew update 以使自制软件再次运行

【讨论】:

  • 酷,它可以找到我以前的数据库吗?
  • 可能不会,尤其是因为您的服务器无法启动。
  • 使用find 和/或locate 不难找到旧的服务器安装。查找 pg.conf 文件或 pg_config 并仔细查看它们。
  • 那么,一旦找到旧安装,我该如何恢复呢?
【解决方案2】:

如果你是通过自制软件安装的,试试这个(来自brew info postgresql

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

这将重新加载它。 Postgress.app 我的默认设置不会找到您的数据库(您需要将其指向PGDATA 目录,并且您可能会遇到版本冲突(如果您运行 9.2 并且 postgress.app 是 9.3,则转储 /restore 将是按顺序排列。

从 cmets 中提取。

好吧,看起来你已经安装了 9.2.3,我会做的是这个。

postgres -D /usr/local/var/postgres 

然后将它们全部备份。 pg_dumpall > ~/mydatabases.dump

杀死postgres

a brew reinstall postgresql ,但警告这会将您从 9.2.3 带到 9.3.1。然后重新导入所有数据库psql < mydatabaes.dump。确保遵循有关 launchctl 内容的卸载/加载说明,此时您应该会很好。您还可以查看使用 postgress.app 并将您的数据库导入该应用程序,但无论如何您都需要备份/转储您的数据库。

【讨论】:

  • 太棒了,看起来我确实使用了自制软件 - brew info postgresql 返回 postgresql: stable 9.3.1... 我如何将它指向 PGDATA driectory?
  • 它是否显示为正在安装类似usr/local/Cellar/postgresql/9.3.1 (2919 files, 39M) * Built from sourcebrew list | grep postgresql 以确保它已安装,如果它只是在launchctl 上执行加载/卸载应该将其恢复,不需要指向 PGDATA,因为它应该已经由启动脚本完成。
  • 我没有得到卸载和加载命令的此类文件或目录。
  • brew info: postgresql: stable 9.3.1 postgresql.org 与:postgres-xc /usr/local/Cellar/postgresql/9.2.3(2824 个文件,39M)冲突 *来自:github.com/mxcl/homebrew/commits/master/Library/Formula/…
  • 你需要安装/启动新服务器。
【解决方案3】:

尝试将其添加到您的 .bash_profile 中,如 here 所述:

export PGHOST=localhost

【讨论】:

    【解决方案4】:

    如果您通过 .dmg 安装,您可以从 Postgres 安装程序(在我的情况下为 postgresql-9.1.3-1-osx.dmg)重新安装。也可能需要重新启动。我昨天才这样做 - 我的实际数据库没有被删除,现在一切都很好。

    从雪豹到山狮也发生了类似的事情:

    Mountain Lion Postgres could not connect

    【讨论】:

      【解决方案5】:

      tl;dr 只需启动 Postgres 应用程序!

      FWIW,我在 Mavericks 升级后的经历完全相同:

      • 安装和启动 9.3(在我的应用程序文件夹中显示为 Postgres93)
      • 将 9.2 移至回收站
      • 看到 Rails 失败,因为我的数据库没有迁移

      然后我又回去了:

      • 退出 9.3
      • 从垃圾箱中恢复 9.2
      • 9.2 启动

      一切正常!

      然后我意识到

      • 问题是我的 Mavericks 安装后没有运行 Postgres
      • 我之前从未做过任何明确的事情来导致 Postgres 在启动时运行,但是 ...
      • Mac OS 总是在重新启动后重新启动 Postgres,因为它之前正在运行

      【讨论】:

        【解决方案6】:

        遇到了同样的问题。发现升级很难杀死进程并将postgres pid文件放在那里。所以,不要像我一样笨,点击重启来升级你的 OSX。确保在升级之前彻底关闭所有内容。

        【讨论】:

        • 使用 Homebrew 安装的 Postgres PID 文件为 /usr/local/var/postgres/postmaster.pid
        【解决方案7】:

        如果您使用 Homebrew 升级,问题可能是数据库格式。直接运行 postgres 命令查看:

        $ postgres
        FATAL:  database files are incompatible with server
        DETAIL:  The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.4.
        

        由于我的开发数据库都是种子数据,我只是把它们吹走了。

        $ rm -rf /usr/local/var/postgres
        $ initdb
        

        当然,由于这会杀死一切,因此您需要在 rake db:create 成功之前重新创建角色。

        $ createuser -s MyApp
        

        如果您不知道角色名称,只需运行 rake db:create。它会告诉你的。

        【讨论】:

          【解决方案8】:

          我最近升级到 10.9.4 时遇到了这个问题

          经过一天的反复试验,我找到了 Joho 的这篇文章,解决了我的问题:https://gist.github.com/joho/3735740

          确保更改版本以匹配您的安装版本。如果您像我一样安装了多个版本,您可以通过访问 cd /usr/local/Cellar/postgresql 查看哪些版本然后执行以下步骤,但更改版本号以匹配您的安装。

          总结:

          $ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
          $ mv /usr/local/var/postgres /usr/local/var/postgres9.2
          $ brew update
          $ brew upgrade postgresql
          $ initdb /usr/local/var/postgres -E utf8
          $ pg_upgrade -b /usr/local/Cellar/postgresql/9.2.1/bin -B /usr/local/Cellar/postgresql/9.3.4/bin -d /usr/local/var/postgres9.2 -D /usr/local/var/postgres
          $ cp /usr/local/Cellar/postgresql/9.3.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
          $ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
          

          如果您使用的是 ruby​​,还可以:

          $ gem pristine pg
          

          我希望这会有所帮助!它对我有用

          【讨论】:

            【解决方案9】:

            在使用homebrewOS X Mavericks 安装PostgreSQL 后,这对我有用:

            sudo ARCHFLAGS="-arch x86_64" gem install pg
            

            如果不是这种情况,那么这个命令已经解决了我的问题,在将操作系统升级到OS X YosemiteOS X El Capitan 后再次发生时

            sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/[Version-Number]/bin gem install pg -v [gem version]
            

            另外,请确保您已将 Xcodecommand line tools 更新为 Xcode

            【讨论】:

              【解决方案10】:

              如果您在从 Yosemite 升级后发现此页面,则需要使用不同的方法来解决问题。本质上,升级到优胜美地会破坏一些文件。你可以找到问题和解决方法here!。

              【讨论】:

                猜你喜欢
                • 2012-03-16
                • 2011-10-09
                • 2014-08-05
                • 2014-03-21
                • 2014-04-28
                • 2013-11-16
                • 1970-01-01
                • 2014-08-21
                相关资源
                最近更新 更多