升级到OSX Mavericks后修复postgresql

最近升级到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中设置host: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一起添加。 我如何找到它的位置? 它很可能是用自制软件安装的,但我不确定。

如果你通过自制软件安装它,试试这个(来自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,则转储/恢复将按顺序排列) 。

从评论中拉出来。

好吧所以看起来你安装了9.2.3,我会做的就是这个。

 postgres -D /usr/local/var/postgres 

然后把它们全部拿回来。 pg_dumpall > ~/mydatabases.dump

杀死postgres

brew reinstall postgresql ,但警告这将带你从9.2.39.3.1 。 然后重新导入所有数据库psql < mydatabaes.dump 。 确保遵循卸载/加载启动项目的指示,在那一点上你应该是好的。 您还可以查看使用postgress.app并将数据库导入该应用程序,但无论如何您还需要备份/转储数据库。

tl; dr刚启动Postgres应用程序!

FWIW,我在小牛队升级之后和以下之后获得了完全相同的体验:

  • 安装和启动9.3(在我的Application文件夹中显示为Postgres93)
  • 将9.2移至垃圾箱
  • 看到Rails失败,因为我的数据库没有迁移

然后我回去了:

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

一切正常!

然后我意识到了

  • 问题是在我的小牛队安装之后,Postgres没有被运行
  • 我之前从未做过任何明确的事情,导致Postgres在启动时运行,但……
  • Mac OS刚刚重启后重启Postgres因为它以前运行过

有同样的问题。 发现升级很难杀死进程并将postgres pid文件留在那里。 所以,不要像我这样的dumbazz并点击重启来升级你的OSX。 确保在升级之前干净地关闭所有内容。

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

要通过自制软件安装,请务必进行brew update以使homebrew再次运行

尝试将此添加到您的.bash_profile中,如下所示:

 export PGHOST=localhost 

当我升级到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 

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

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

从Snow Leopard到Mountain Lion也发生了类似的事情:

Mountain Lion Postgres无法连接

如果使用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。 它会告诉你。

如果您在从Yosemite升级后找到此页面,则需要采用不同的方法来解决问题。 基本上升级到Yosemite会破坏一些文件。 你可以在这里找到问题和解决方案!

使用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