重新启动我的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启动,但进程未运行

其他回答

对于那些在M1 mac上遇到这个问题的人,尝试删除这个文件,然后重新启动brew服务:

rm /opt/homebrew/var/postgres/postmaster.pid

我刚刚得到了相同的问题,因为我已经把我的机器(ubuntu)更新,并得到以下错误:

无法连接到服务器:没有这样的文件或目录是服务器吗 在Unix域套接字上本地运行并接受连接 “/ var /运行/ postgresql / .s.PGSQL.5432”?

完成更新过程后,当我重新启动系统时,我的错误消失了。它的工作就像以前一样迷人。我猜这是在pg更新和另一个进程启动时发生的。

以上这些方法对我都没用。我必须以以下方式重新安装Postgres:

使用brew卸载postgresql 酿造医生(修复这里的任何东西) 啤酒清理 删除所有Postgres文件夹: Rm -r /usr/local/var/postgres rm -r ~/库/应用\支持/Postgres . txt 使用brew重新安装postgresql 启动服务器:brew服务启动postgresql 你现在必须创建你的数据库…(createdb)

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

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

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

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

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

超级新手提醒:我正在学习web开发,我正在学习的教程中提到我必须安装Postgres,但实际上并没有提到我必须运行它……一旦我打开Postgres应用程序,一切都很好。