Ruby-on-rails 升级到 OSX Mavericks 后修复 postgresql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19565708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:55:30  来源:igfitidea点击:

Repairing postgresql after upgrade to OSX Mavericks

ruby-on-railsrubymacospostgresqlosx-mavericks

提问by Ryan King

A recent upgrade to OSX Mavericks has broken my database connection for my Rails app.

最近升级到 OSX Mavericks 破坏了我的 Rails 应用程序的数据库连接。

When I try to fetch from the database the server returns the following error:

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

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?

When try to run psqlI get:

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


I've tried many of the solutions available on the internet. Such reinstalling the pg gem and setting host: localhost in my database.yml. My /usr/local/var/postgres/pg_hba.conf file says:

我已经尝试了许多互联网上可用的解决方案。这样重新安装 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

which psql returns: /usr/local/bin/psql

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



Any solutions on this one? Some solutions suggest I need to change my $PATH to my previous postgres installation as a new version of postgres would be added with Mavericks. How do I find where that is located? It's quite possible it was installed with homebrew but I'm not certain.

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

回答by Doon

if you installed it via homebrew, try this (from brew info postgresql)

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

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

Which will reload it. Postgress.app my default will not find your databases (you would need to point it to the PGDATAdirectory, and you might run into version conflicts (if you where running 9.2 and postgress.app is 9.3, a dump /restore would be in order.

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

pulling from comments.

从评论中提取。

ok so it looks like you have 9.2.3 installed, what I would do is this.

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

postgres -D /usr/local/var/postgres 

Then back them all up. pg_dumpall > ~/mydatabases.dump

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

kill postgres

杀死 postgres

a brew reinstall postgresql, but warning this will bring you from 9.2.3to 9.3.1. Then reimport all your databases psql < mydatabaes.dump. Making sure to follow the directions on the unload/load for the launchctl stuff, and at that point you should be good. You could also look at using postgress.app and importing your databases into that app, but you would need to backup/dump your database anyway.

a brew reinstall postgresql,但警告这将使您从9.2.39.3.1。然后重新导入所有数据库psql < mydatabaes.dump。确保按照关于 launchctl 内容的卸载/加载说明进行操作,此时您应该会很好。您还可以考虑使用 postgress.app 并将您的数据库导入该应用程序,但无论如何您都需要备份/转储您的数据库。

回答by Peter Alfvin

tl;dr Just start up the Postgres application!

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

FWIW, I had the exact same experience after my Mavericks upgrade and after:

FWIW,我在小牛队升级后和之后有完全相同的经历:

  • Installing and starting up 9.3 (appeared as Postgres93 in my Application folder)
  • Moving 9.2 to the trash
  • Seeing Rails fail because my databases weren't migrated
  • 安装和启动 9.3(在我的应用程序文件夹中显示为 Postgres93)
  • 将 9.2 移至垃圾箱
  • 看到 Rails 失败,因为我的数据库没有迁移

I then went back and:

然后我回去:

  • Quit 9.3
  • Restored 9.2 from the trash
  • Started up 9.2
  • 退出 9.3
  • 从垃圾箱中恢复 9.2
  • 启动 9.2

and everything worked fine!

一切正常!

I then realized that

然后我意识到

  • The problem was that Postgres hadn't been run after my Mavericks install
  • I'd never done anything explicit before to cause Postgres to be run at startup, but ...
  • Mac OS was just always restarting Postgres after a reboot because it was running previously
  • 问题是 Postgres 在我的 Mavericks 安装后没有运行
  • 我以前从未做过任何明确的事情来导致 Postgres 在启动时运行,但是......
  • Mac OS 只是在重新启动后总是重新启动 Postgres,因为它以前运行过

回答by GregD

Had the same problem. Found that the upgrade killed the processes hard and left the postgres pid file out there. So, don't be a dumbazz like me and click on the restart to upgrade your OSX. Make sure you shutdown everything cleanly before the upgrade.

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

回答by jewilmeer

The simplest solution is to use http://postgresapp.comwhich just works

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

To install via homebrew, make sure to do brew updateto get homebrew working again

要通过自制软件安装,请确保brew update让自制软件再次工作

回答by Daniel Ristic

Try to add this to your .bash_profile as stated here:

试试按规定这个添加到你的.bash_profile在这里

export PGHOST=localhost

回答by Brent11

I recently had this issue happen to me when I upgraded to 10.9.4

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

After a day of trial and error, I found this post by Joho which fixed my problems: https://gist.github.com/joho/3735740

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

Make sure you change the versions to match your installation versions. If you have multiple versions installed like I did, you can see which versions by accessing cd /usr/local/Cellar/postgresql Then do the steps below but change the version numbers to match your installs.

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

In Summary:

总之:

$ 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

If you're using ruby, also:

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

$ gem pristine pg

I hope this helps! It did for me

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

回答by Neil Billingham

If you install via the .dmg you can just reinstalled from the Postgres installer (postgresql-9.1.3-1-osx.dmg in my case). A reboot might be required too. I did this just yesterday - my actually databases didn't get deleted and all is fine now.

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

A similar thing happened going from Snow Leopard to Mountain Lion too:

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

Mountain Lion Postgres could not connect

Mountain Lion Postgres 无法连接

回答by Lahiru

After installing PostgreSQLusing homebrewfor OS X Mavericks, this worked for me:

PostgreSQL使用homebrewfor安装后OS X Mavericks,这对我有用:

sudo ARCHFLAGS="-arch x86_64" gem install pg

If that's not the case, then this command has fixed the issue for in my case once it occurred again after upgrade OS to OS X Yosemiteand OS X El Capitan

如果情况并非如此,那么此命令已修复了我的问题,一旦在将操作系统升级到OS X Yosemite和之后再次发生OS X El Capitan

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

Also, Make sure you have updated your Xcodeand command line toolsfor Xcode.

另外,请确保您已更新您的Xcodecommand line toolsfor Xcode

回答by Obromios

In case you have found this page after upgrading from Yosemite, then a different approach is needed to solve the problem. Essentially the upgrade to Yosemite destroys some files. You can find the problem and solution here!.

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

回答by bronson

If you upgraded using Homebrew, the problem is probably the database format. Check it by running the postgres command directly:

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

Since my development databases are all seed data, I just blew them away.

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

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

Of course, since this kills everything, you'll need to re-create your roles before rake db:create can succeed.

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

$ createuser -s MyApp

If you don't know the role name, just run rake db:create. It will tell you.

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