当我试图在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
OR
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.
对于根执行错误,我必须运行以下命令来修复文件权限,当我用sudo命令前缀运行brew服务时,文件权限被打乱了。将你的用户名替换为你的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)/*
由于数据库文件与服务器不兼容,我不得不简单地将现有的PostgreSQL数据文件(使用版本13创建的)升级到我计算机上的最新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启动,但进程未运行