重新启动我的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。


当前回答

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

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

其他回答

我的问题是,我使用的是Gas Mask (Mac主机文件管理器),在我使用的hosts文件中没有localhost的条目。

我补充说:

127.0.0.1 localhost

这解决了我的问题。

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

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

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

对我来说,最好但奇怪的方式是做下一步的事情。 1)下载postgres93。App或其他版本。将此应用程序添加到/Applications/文件夹中。

2)在.bash_profile文件(在我的主目录下)中添加一行(命令): 导出路径= /应用程序/ Postgres93.app /内容/ MacOS / bin /: $路径 这是Postgres93.app中psql的路径。每次启动控制台时,行(命令)都会运行。

3)启动Postgres93。app从/Applications/文件夹。它启动一个本地服务器(端口为“5432”,主机为“localhost”)。

4)在所有这些操作之后,我很高兴运行$ createuser -SRDP user_name和其他命令,并看到它工作!Postgres93。应用程序可以在每次系统启动时运行。

5)如果你想看到你的数据库图形化,你应该安装PG Commander.app。这是将postgres DB视为漂亮的数据表的好方法

当然,它只对本地服务器有帮助。如果这些说明能帮助到其他面临这个问题的人,我会很高兴。

我在这里遇到了类似的问题,我解决了这个问题,如下所示。

实际上postgres进程已经死亡,要查看postgres的状态,请运行以下命令

sudo /etc/init.d/postgres status

它会说进程已死,只要启动进程

sudo /etc/init.d/postgres start

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