尝试在MacOS Monterey上启动 PostgreSQL 数据库服务器时,我也有同样的担忧。
当我运行以下命令重新启动 PostgreSQL 数据库服务器时:
brew services restart PostgreSQL
它重新启动,但是当我尝试使用以下命令检查 PostgreSQL 数据库服务器的状态时,出现错误:
Name Status User File
mysql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
postgresql error 256 root ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
这对我有用:
首先,我使用以下命令检查了 PostgreSQL 数据库服务器的日志文件,以了解错误的原因:
cat /usr/local/var/log/postgres.log
或者
nano /usr/local/var/log/postgres.log
日志显示以下错误:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
2022-01-25 19:01:06.051 WAT [29053] FATAL: database files are incompatible with server
2022-01-25 19:01:06.051 WAT [29053] DETAIL: The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.1.
对于根执行错误,我必须运行以下命令来修复当我brew services
使用sudo
命令前缀运行时弄乱我们的文件权限。替换your-username
为您的 MacOS 用户名(在我的情况下,我的用户名是promisepreston
:
# 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 your-username:admin $(brew --prefix)/*
由于数据库文件与服务器不兼容,我必须简单地将使用版本 13 创建的现有 PostgreSQL 数据文件升级到我计算机上的最新 PostgreSQL 版本,即 14.1,方法是运行以下命令:
brew postgresql-upgrade-database
之后,我重新启动了 PostgreSQL 数据库服务器:
brew services restart PostgreSQL
然后使用以下命令检查状态:
brew services list
然后我得到下面的输出,表明一切正常:
Name Status User File
mysql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
postgresql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
就这样。
参考: Brew Postgresql 启动但进程未运行