重新启动我的Mac,我得到了可怕的Postgres错误:

psql: could not connect to server: No such file or directory
 Is the server running locally and accepting
 connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

发生这种情况的原因是由于一个无关的问题,我的macbook完全死机了,我不得不使用电源按钮进行硬重启。重启后,由于这个错误,我无法启动Postgres。


当前回答

当我试图在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启动,但进程未运行

其他回答

导致这种情况发生的另一类原因是由于Postgres的版本更新。

你可以通过查看postgres日志来确认这是一个问题:

tail -n 10000 /usr/local/var/log/postgres.log

看到这样的条目:

DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.

在这种情况下(假设你在Mac上使用brew),只需运行:

brew postgresql-upgrade-database

(奇怪的是,它在第一次运行时失败了,在第二次运行时工作了,所以在放弃之前尝试两次)

为了解决这个问题,我不得不多次查阅这篇文章。还有另一个修复程序,它也适用于其他正在运行的程序的类似问题。

我刚刚发现您可以使用以下两个命令。

美元上

这将显示当前运行的所有操作及其pid编号。当你找到postgres和相关的pid

杀死pid_number美元

这个问题有很多来源,因此也有很多答案。每一次我都经历过。

1)如果你有某种类型的崩溃,删除/usr/local/var/postgres/postmaster。Pid文件可能是必需的,因为postgres可能没有正确处理它。但是确保没有进程正在运行。

2) Craig Ringer在其他文章中指出,苹果捆绑postgreSQL导致pg gem安装问题,设置PATH环境变量是一个解决方案。

3)另一个解决方案是卸载并重新安装gem。酿造更新可能也是必要的。

如果你偶然发现了这篇文章,如果你能找到其中一个来源,你将节省时间……

这发生在我的Mac (High Sierra)死机后,我不得不手动重启它(按住电源按钮)。我所要做的就是重新开始。

我也遇到了同样的问题,这是由于从版本11升级到13.2后版本不兼容

检查错误日志:

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

给我:

DETAIL:  The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 13.2.

为了解决这个问题,我跑了:

brew postgresql-upgrade-database

然后用brew启动postresql:

brew services start postgresql