我已经在 Mac 上通过 Brew 安装了 Postgres。然后,我尝试启动它
| => brew services restart postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
之后,我检查进程是否正在运行
(base) ________________________________________________________________________________
| ~/ @ MacBook-Pro (user)
| => ps -ef | grep postgres
501 61568 561 0 5:00PM ttys000 0:00.00 grep postgres
此外,
brew services
产生以下
| => brew services
Name Status User Plist
dnsmasq unknown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
emacs stopped
postgresql error root /Library/LaunchDaemons/homebrew.mxcl.postgresql.plist
postgresql@12 stopped
rabbitmq stopped
unbound stopped
任何有关如何解决此问题的帮助将不胜感激。
与 Linux 中的内置 pkg 管理器不同,macOS 上的 Homebrew 在大多数
root
命令期间不需要 brew
权限。唯一的例外是当您启动服务侦听端口时 sudo brew services
< 1024.回答你的问题:你用 sudo brew services
搞乱了文件权限。该解决方案适用于 x86 Mac。我还没有在 M1 mac 上测试过。 (Homebrew 在 x86 Mac 上将相关文件存储在
/usr/local
下,在 M1 Mac 上将相关文件存储在
/opt/homebrew
下。)
### Run in Bash or Zsh
# Stop postgresql
sudo brew services stop postgresql
# In case service file is copied to ~/Library/LaunchAgents as well
brew services stop postgresql
# Fix permission of homebrew files
sudo chown -R $USER:admin $(brew --prefix)/*
# Remove socket like `/tmp/.s.PGSQL.5432`
# Restart postgresql without sudo
brew services start postgresql
除了上述修复之外,您可能还需要
brew info postgresl
了解详细信息
# Change /usr/local to /opt/homebrew on M1 Mac
# Check the start log
pg_ctl -D /usr/local/var/postgres start
# to stop it
pg_ctl -D /usr/local/var/postgres stop
postgresql
不能以 root 身份启动,但 Homebrew 和
brew services
并非设计用于与
sudo
一起使用。并且
sudo brew services
将某些文件的所有者更改为
root
。你最好把它们改回你自己所有。这是一个例子(检查警告部分)
❯ sudo brew services start adguardhome
Warning: Taking root:admin ownership of some adguardhome paths:
/usr/local/Cellar/adguardhome/0.106.3/bin
/usr/local/Cellar/adguardhome/0.106.3/bin/AdGuardHome
/usr/local/opt/adguardhome
/usr/local/opt/adguardhome/bin
/usr/local/var/homebrew/linked/adguardhome
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: adguardhome must be run as non-root to start at user login!
==> Successfully started `adguardhome` (label: homebrew.mxcl.adguardhome)
sudo brew services
不够智能,无法自动将文件的 onwer bach 更改为
$USER
。我编写了一个脚本来执行此操作:。但这对于您的情况来说还不够。您必须将
$(brew --prefix)/var/postgres
下的 postgres 配置文件的所有权改回。这就是为什么我放了
sudo chown -R $USER:admin $(brew --prefix)/*
。相关解答
sudo brew services
启动MySQL。关于
brew services
如何工作的额外详细答案是there。您可能想要一张支票。
brew services list
来停止所有正在运行的brew服务,以查看所有正在运行的服务,并通过运行一个接一个地停止它们
brew services stop service-name
通过运行此命令更改自制目录的所有权
sudo chown -R "$(whoami)" "$(brew --prefix)"/*
更改服务数据目录的所有权,例如 postgres
sudo chown -R "$(whoami)" "$(brew --prefix)/var/postgres"
最后,启动postgres服务
brew services start postgres
运行brew services list
,你会看到postgres用户已从root更改为当前用户
brew services stop postgresql
sudo chown -R $(whoami) $(brew --prefix)/*
brew services start postgresql