Homebrew安装的Postgres出现问题

94
我在我的Mac(10.10.1/Yosemite)上使用homebrew安装了Postgresql 9.4.0,但它无法运行。
我已经在~/Library/LaunchAgents中创建了到/usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist的软链接。
如果我尝试手动加载postgres,我会收到“操作正在进行中”的消息。
> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.0/homebrew.mxcl.postgresql.plist: Operation already in progress

然而,Postgres似乎没有在运行。
> ps auxw | grep post
billmcn           670   0.0  0.0  2424272    452 s000  R+   10:12PM   0:00.01 grep post

我无法使用命令行客户端连接。
> 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"?

据我所知,我尝试了other Stackoverflow上讨论此问题的所有建议解决方法。具体来说:
  • 我已卸载并重新安装了postgres和相应的Ruby gem。我的机器上没有postgres 8.0版本。
  • 我已验证psql客户端程序是Homebrew安装的9.4.0版本,而不是Mac系统二进制文件。
  • 我已验证/usr/local/var/postgres/postmaster.pid不存在。
  • 我已重启了机器。

我之前在这台机器上成功使用过Homebrew postgres。我认为导致它出现问题的原因是从版本8升级到版本9,但我不确定。

我没有需要保留的数据库。我愿意用干净的postgres开始;我现在只需要让它工作。有什么想法吗?


这个问题似乎是由于 /usr/local/var/postgres 目录权限不正确所致。以下是在出现问题时我的 var 目录的情况。
ll /usr/local/var/
drwxr-xr-x  3 billmcn  admin  102 Dec 20 12:44 cache
drwxr--r--  2 root     admin   68 Dec 29 21:37 postgres

(whoami = "billmcn")

我是"billmcn"。

我删除了 /usr/local/var/postgres,卸载并重新安装了 postgres,现在它看起来像这样。

ll /usr/local/var/
drwxr-xr-x   3 billmcn  admin  102 Dec 20 12:44 cache
drwx------  23 billmcn  admin  782 Dec 30 10:51 postgres

我不确定它是如何进入这种状态的,因为我不记得在此目录上调整权限,但无论如何,现在它可以正常工作了。

12个回答

132

在最开始安装 Yosemite 操作系统时,我使用 homebrew 安装 postgres 出现了相同的问题。

首先,我的 brew 配置如下:

HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: 9f6926265f8e4be7cc80dfe9042f2cd3c1e8dc9e
Last commit: 64 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
CPU: quad-core 64-bit sandybridge
OS X: 10.10.1-x86_64
Xcode: 6.1.1
Clang: 6.0 build 600
X11: N/A
System Ruby: 2.0.0-481
Perl: /usr/bin/perl
Python: /usr/bin/python
Ruby: ~/.rvm/rubies/ruby-2.1.1/bin/ruby

我注意到的第一件事是我没有对/usr/local/var/postgres有写入权限。这很容易通过输入sudo chown -R `whoami` /usr/local/var/postgres来改变,然后我重新安装了postgresql并运行了

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

查看错误信息:

postgres cannot access the server configuration file "/usr/local/var/postgres/postgresql.conf": No such file or directory

所以我移除了目录/usr/local/var/postgres并执行了初始化数据库的命令。

initdb -D /usr/local/var/postgres/

这似乎解决了问题,现在postgresql正在正常运行。


2
哦,我的天啊!非常感谢。我已经为此苦苦挣扎了两个小时。我以为Homebrew应该简化那个安装过程。 - fro_oo
1
我希望我能够给这个+5。 - Geoffrey Burdett
initdb:无法更改目录“/usr/local/var/postgres”的权限:操作不允许 - Herb Meehan
请执行以下命令:sudo chown -R whoami /usr/local/var/postgres,然后运行initdb。 - Suge
在升级操作系统后,我第二次遇到这个问题 - 如果brew postgres可以自动处理这个问题,那就太好了。 - Andreas
显示剩余8条评论

66

我也遇到了同样的问题。主要问题是,在Mac上,安装的initdb步骤将使用root所有权创建目录,而不是用户所有权。为解决此问题:

在运行initdb之前创建数据目录并设置权限为0700。

rm -rf /usr/local/var/postgres  # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres

然后运行initdb,它将遵守数据目录的权限。

initdb -D /usr/local/var/postgres

为了好玩,创建一个以您的用户名命名的测试数据库:

createdb `whoami`

登录进行测试:

psql

我运行了 rm -rf /usr/local/var/postgres 和/或 sudo rm -rf /usr/local/var/postgres 命令,两者都会删除postgres目录,然后我手动添加一个新目录,但是出现 mkdir: /usr/local/var/postgres: File exists 的错误!? psql 仍显示“无此文件或目录”。关于我的情况的上下文:http://stackoverflow.com/questions/31088325/how-can-i-migrate-postgres-9-3-5-to-9-4-4-after-deleting-the-9-3-5-oldbindir - jbk
我在我的 M1 Mac 上也必须遵循相同的过程,但使用 brew 路径,对我来说是: /opt/homebrew/var/postgres - rii

14

使用Homebrew尝试安装postgresql后,我遇到了以下问题:

Warning: postgresql-9.5.2 already installed, it's just not linked

所以我尝试了:

brew link postgresql

并且遇到了这个错误:

Linking /usr/local/Cellar/postgresql/9.5.2... 
Error: Could not symlink share/man/man3/SPI_connect.3
/usr/local/share/man/man3 is not writable.

似乎是写入权限的问题,所以我执行了以下操作:

sudo chown -R `whoami` /usr/local/share/man/

这很管用,因为我随后可以成功地执行以下操作(无错误):

brew link postgresql

13

如果有人从之前的版本升级,请不要忘记:

brew postgresql-upgrade-database

通过将现有数据库升级到您升级PostgreSQL的版本,即可解决该问题。


这个对我有用,问题解决了。 - Micah
哇!我已经在谷歌上搜索了几个小时!这解决了我的问题! - Paul Tuckett

11
请注意,在Homebrew的Github上有一个处理此问题的主题讨论:https://github.com/Homebrew/homebrew/issues/35240
我遇到了类似的问题。James的答案帮助我解决了它。但是,我接着遇到了jbk提到的问题(在删除了 /usr/local/var/postgres 后,它仍然被重新创建)。
这个问题是因为如果你创建了符号链接:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

并启动了该流程:

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

您首先应该卸载它:

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

在运行詹姆斯的命令之前。

rm -rf /usr/local/var/postgres  # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres

此外,如果像我一样,您有一个用于管理 Homebrew 的管理员用户和一个将使用 pgsl 进行开发的普通用户,则应以超级用户身份运行 James 命令:

sudo -s

并且PostgreSQL目录的所有权应该授予您的开发用户:

chown my-dev-user /usr/local/var/postgres

接下来的命令,以dev用户身份运行,应该可以正确地填充目录:

createdb `whoami`

运行中:

psql -l

在进行这样的操作后,应该向您展示PostgreSQL中的表格和用户权限。

希望这有所帮助。


1
这太棒了!谢谢Cedric。我也遇到了权限的问题。 - thesayhey

5

在日志文件中看到此信息后,我不得不删除 .pid 文件。

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

2021-10-10 19:05:27.468 BST [41868] FATAL:  lock file "postmaster.pid" already exists
2021-10-10 19:05:27.468 BST [41868] HINT:  Is another postmaster (PID 820) running in data directory "/usr/local/var/postgres"?


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

我使用brew进行安装


我不知道为什么这个问题会出现,但这也解决了我的问题。 - Burak Kaymakci

4

基于 @James 的回答,这是我在我的M1 Monterey机器上所做的事情。对于我来说,目录不同。

危险:在评论中指出了我的脚本删除了数据库。

在终端中:

#to fix postgresql of existing installation
cd /opt/homebrew/var
rm -rf postgres
mkdir postgres
chmod 0700 postgres
initdb -D postgres

#install postgres
echo "installing postgres..."
brew install postgresql
brew services restart postgresql
createuser postgres -s

我随后可以运行brew install --cask pgadmin4,然后从应用程序中运行pgadmin并连接到127.0.0.1。


这个可行!非常感谢。 - Damagedcase
1
这个可以运行,但不幸的是它也会删除整个数据库。 - Jeremy Lynch
在文本中添加了一个危险的数据库删除。 - CodingYourLife

2

我在电脑因为停电而关闭后遇到了这个问题。

# This initialize your database with the current data and settings
initdb -D postgres

# This will start database service
pg_ctl -D postgres -l logfile start

2

最近我遇到了一个问题,当我升级一些brew更新/升级时,主要是Python版本等等。以下是对我有用的方法。

brew uninstall postgres
brew install postgresql@9.5
echo 'export PATH="/usr/local/opt/postgresql@9.5/bin:$PATH"' > ~/.zshrc
# you may need > ~/.bashrc if you use bash

我需要使用pg_dump、pg_restore等工具,为了让它们正常工作,我执行了以下操作:

brew install libpq

启动服务

brew services start postgresql@9.5

从这里开始,我本来期望一切都能正常工作,但是所有的Rails db命令仍然报错,提示服务器没有运行。最终解决问题的关键在于找到了这个缺失的部分。

gem uninstall pg
gem install pg -v 0.20.0 # which was set in Gemfile
# could also just probably do bundle install instead.

1

为了后人,我遇到了这个问题,并想记录一下对我有用的解决方法。

我在 High Sierra 上运行 postgres 11.2。我最近通过 brew postgresql-upgrade-database 从 postgres 10 升级。

我不断收到错误信息 psql: could not connect to server: No such file or directory,我的 server.log 指出 is another postmaster (PID 5894) running in data directory "/usr/local/var/postgres"?

我尝试了几种解决方案,包括重新启动计算机、删除 postmaster.pid、使用 brew services restart postgres,但都无济于事。最终我偶然发现了解决方法:

brew unlink postgresql && brew link postgresql

不知道为什么这个方法有效,但我把它放在这里主要是为了以后自己参考!把东西扔到墙上看看会不会粘上去吧!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接