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


当前回答

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

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

其他回答

对我来说,最好但奇怪的方式是做下一步的事情。 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视为漂亮的数据表的好方法

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

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

我不完全确定为什么,但我的Postgres安装有点搞砸了,一些文件被删除,导致错误OP显示。

尽管我能够运行像brew service restart postgres这样的命令并看到正确的消息,但这个错误仍然存在。

我浏览了postgres文档,发现我的文件/usr/local/var/postgres完全是空的。所以我运行了以下程序:

initdb /usr/local/var/postgres

这个命令似乎发生了一些配置。

然后它让我运行这个:

postgres -D /usr/local/var/postgres

这告诉了我一个邮政局长。Pid文件已经存在。

我只是需要知道brew是否能够拾取我刚刚运行的配置,所以我测试了它。

ls /usr/local/var/postgres

这让我想到了邮政局长。pid文件。然后我做了brew服务停止postgresql,和postmaster。Pid文件消失。然后我做了brew服务启动postgresql和VIOLA,文件重新出现。

然后我继续运行我的应用程序,它确实找到了服务器,但我的数据库似乎消失了。

尽管我知道它们可能根本没有消失——我所做的新的初始化可能创建了一个新的data_area,而旧的data_area没有被指向。我必须看看它在哪里,并把它点回来,或者只是重新创建我的数据库。

希望这能有所帮助!阅读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启动,但进程未运行

警告:如果您删除postmaster。Pid,而不确定是否真的有postgres进程在运行你,可能会永久破坏你的数据库。(PostgreSQL应该自动删除它,如果postmaster已经退出。)

解决方案:这解决了问题——我删除了这个文件,然后一切工作!

/usr/local/var/postgres/postmaster.pid

--

这就是我是如何发现为什么这个需要删除的。

我使用以下命令查看是否有任何PG进程正在运行。对我来说,没有,我甚至无法启动PG服务器: Ps auxw | grep post 我搜索了. s.p gsql文件。5432在上面的错误消息中。我使用了以下命令: sudo find / -name . s.p gsql。5432 - ls

在搜索了我的整个计算机后,这没有显示任何东西,所以文件不存在,但显然PSQL“想要它”或“认为它在那里”。

我看了一下我的服务器日志,看到了以下错误: 猫/usr/local/var/postgres/server.log

在服务器日志的末尾,我看到以下错误:

    FATAL:  pre-existing shared memory block (key 5432001, ID 65538) is still in use
    HINT:  If you're sure there are no old server processes still running, remove the shared memory block or just delete the file "postmaster.pid".

按照错误消息中的建议,我删除了邮政局长。Pid文件与server.log在同一目录下。这解决了问题,我可以重新启动。

所以,似乎我的macbook被冻结和硬重启导致Postgres认为它的进程在重启后仍在运行。删除此文件已解决。很多人都有类似的问题,但大多数答案都与文件权限有关,而我的情况则不同。