Ruby-on-rails 升级到 OSX 10.7 Lion 后修复 Postgresql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6770649/
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-03 01:38:33  来源:igfitidea点击:

Repairing Postgresql after upgrading to OSX 10.7 Lion

ruby-on-railsruby-on-rails-3macospostgresqlosx-lion

提问by Dave G

I recently upgraded to OSX 10.7, at which point my rails installation completely borked when trying to connect to the psql server. When I do it from the command line using

我最近升级到 OSX 10.7,此时我的 rails 安装在尝试连接到 psql 服务器时完全停止。当我从命令行使用

psql -U postgres

it works totally fine, but when I try to run the rails server or console with the same username and password, I get this error

它完全正常,但是当我尝试使用相同的用户名和密码运行 rails 服务器或控制台时,出现此错误

...activerecord-3.0.9/lib/active_record/connection_adapters/postgresql_adapter.rb:950:in `initialize': could not connect to server: Permission denied (PGError) 
Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

Any ideas what might be going on would be super helpful! Thanks!

任何可能发生的事情都会非常有帮助!谢谢!

回答by John Wang

It's a PATH issue. Mac OSX Lion includes Postgresql in the system now. If you do a which psqlyou'll likely see usr/bin/psqlinstead of usr/local/bin/psqlwhich is HomeBrew's correct one. If you run brew doctoryou should get a message stating that you need to add usr/local/binto the head of your PATH env variable.

这是一个 PATH 问题。Mac OSX Lion 现在在系统中包含 Postgresql。如果你做了一个,which psql你很可能会看到HomeBrew 正确的usr/bin/psql而不是usr/local/bin/psql哪个。如果您运行,brew doctor您应该收到一条消息,指出您需要添加usr/local/bin到 PATH 环境变量的头部。

Editing your .bash_profile or .profile, or whichever shell you're using and adding: export PATH=/usr/local/bin:$PATH

编辑您的 .bash_profile 或 .profile,或您使用的任何 shell 并添加: export PATH=/usr/local/bin:$PATH

as the first export for the PATHthen either quit you shell session or source your file with source ~/.bash_profileand it should now be OK again.

作为PATH然后的第一次导出,要么退出您的 shell 会话,要么使用source ~/.bash_profile它来获取您的文件,现在应该又可以了。

回答by Dave G

For those of you who are interested, I pieced together the solution. All I needed was to add

对于那些有兴趣的人,我拼凑了解决方案。我所需要的只是添加

host: localhost

to the database.yml for my environment and all was gravy.

到我的环境的 database.yml,一切都是肉汁。

回答by Ben

I had this very problem with Mountain Lion but the only thing that worked for me was this fix:

我在 Mountain Lion 上遇到了这个问题,但唯一对我有用的是这个修复

Check where the actual target is:

检查实际目标在哪里:

sudo find / -name .s.PGSQL.5432

I needed to create this directory:

我需要创建这个目录:

mkdir /var/pgsql_socket/

Then using the result from the find above create this symlink:

然后使用上面查找的结果创建此符号链接:

ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

I suspect that for most people on Mountain Lion you can just create the dir and do the symlink and not waste time doing the find unless the symlink doesn't work.

我怀疑对于 Mountain Lion 上的大多数人来说,您可以只创建目录并执行符号链接,而不是浪费时间进行查找,除非符号链接不起作用。

PS - my PostgreSQL was installed through the official installer.

PS - 我的 PostgreSQL 是通过官方安装程序安装的。

回答by Darren

If the problem persists past changing the path (as it did for me), also try this...

如果问题在更改路径后仍然存在(就像对我所做的那样),也试试这个......

gem pristine pg

It appears that the problem (partially) lies in the pg gem itself. When it builds, it figures out where the domain socket should be. If you change the location of the domain socket after the fact it doesn't seem to take effect until you rebuild the gem.

问题似乎(部分)出在 pg gem 本身。当它构建时,它会确定域套接字应该在哪里。如果事后更改域套接字的位置,则在重建 gem 之前它似乎不会生效。

回答by Rogerio

For those who installed direct from the official installer, just adding the host to the command works with no path changes:

对于那些直接从官方安装程序安装的人,只需将主机添加到命令即可,无需更改路径:

psql -h localhost -U postgres

回答by vladiim

I had the same issue and was having problems making John Wang's solution work. As Darren noted there's an issue with the pg gem. To get it work I needed to:

我遇到了同样的问题,并且在使 John Wang 的解决方案起作用时遇到了问题。正如达伦所指出的,pg gem 存在问题。为了让它工作,我需要:

gem uninstall pg

Then reinstall.

然后重新安装。

Which got it working.

这让它工作。

回答by Antony Stubbs

Is this for homebrew? Ports seems to put it in:

这是自制软件吗?港口似乎把它放在:

/opt/local/lib/postgresql91 

So make sure you use export

所以请确保您使用导出

PATH=/opt/local/lib/postgresql91/bin:$PATH

Mac ports issue: https://trac.macports.org/ticket/30125

Mac 端口问题:https: //trac.macports.org/ticket/30125

回答by tjarratt

I ran into this as well, but I had installed postgres myself (not with homebrew). If that's the case, you need to find the old path to psql (which may be /usr/local/bin, but for me was /usr/local/pgsql/bin) and prepend that to your $PATH.

我也遇到了这个问题,但我自己安装了 postgres(不是用自制软件)。如果是这种情况,您需要找到 psql 的旧路径(可能是 /usr/local/bin,但对我来说是 /usr/local/pgsql/bin)并将其添加到您的 $PATH 中。

(before) which psql=> /usr/bin/psql

(之前) which psql=> /usr/bin/psql

(fix) export PATH=/usr/local/psql/bin:$PATH

(修复) 导出 PATH=/usr/local/psql/bin:$PATH

(after) `which psql' => /usr/local/psql/bin

(之后)`which psql' => /usr/local/psql/bin

John Wang's suggestion to source ~/.bash_rcafterward you add that to your bash_rc is golden.

John Wang 建议source ~/.bash_rc您稍后将其添加到您的 bash_rc 中,这非常有用。

回答by mike

Another possible solution that worked for me is resetting the postmaster file by deleting it. Simply run:

对我有用的另一个可能的解决方案是通过删除它来重置 postmaster 文件。只需运行:

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

It's worth checking the log for errors which you can find here:

值得检查日志中的错误,您可以在这里找到:

/usr/local/var/postgres/server.log

The error message I was having was:

我遇到的错误消息是:

FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 161) running in data directory 
"/usr/local/var/postgres"?

Everything worked great afterwards.

之后一切都很好。

回答by Eric Hu

I'm not happy with the most upvoted answers as they are either OS-user specific or remap Postgres to use TCP instead of domain sockets, as pointed out by @pilif. I've seen another solution that involves re-ordering the default paths at the system level to check Brew's path before a core system path, but this seems hazardous as it could affect all other application name-collisions like this one.

正如@pilif 所指出的,我对最受好评的答案不满意,因为它们要么是特定于操作系统的用户,要么是重新映射 Postgres 以使用 TCP 而不是域套接字。我见过另一种解决方案,它涉及在系统级别重新排序默认路径以在核心系统路径之前检查 Brew 的路径,但这似乎很危险,因为它可能会影响所有其他应用程序名称冲突,例如此类。

This sitedetails a solution my coworker found. It comes down to executing a single shell script that will

该站点详细介绍了我的同事找到的解决方案。它归结为执行单个 shell 脚本,它将

  1. back up the Postgres 8.4 files in a separate directory
  2. symlink the brew's installation of Postgres in place
  1. 在单独的目录中备份 Postgres 8.4 文件
  2. 将 brew 的 Postgres 安装符号链接到位

This comes with the caveat that the system default Postgres is whatever brew has installed, so you have to make a judgment call about whether that's right for you. I don't see myself needing Postgres 8.4 specifically over 9.x, but YMMV

需要注意的是,系统默认 Postgres 是 brew 安装的任何东西,因此您必须判断这是否适合您。我不认为自己需要 Postgres 8.4,特别是 9.x,但 YMMV