psql:无法连接到服务器:没有这样的文件或目录(Mac OS X)

Fir*_*gon 213 postgresql macos

重启我的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"?
Run Code Online (Sandbox Code Playgroud)

发生这种情况的原因是因为我的macbook完全因为一个无关的问题而冻结,我不得​​不使用电源按钮进行硬重启.重新启动后,由于此错误,我无法启动Postgres.

Fir*_*gon 383

警告:如果删除postmaster.pid而不确定没有正在postgres运行的进程,可能会永久损坏您的数据库.(PostgreSQL应该在postmaster退出时自动删除它.).

解决方案:这解决了问题 - 我删除了这个文件,然后一切正常!

/usr/local/var/postgres/postmaster.pid
Run Code Online (Sandbox Code Playgroud)

-

以下是我如何弄清楚为什么需要删除它.

  1. 我使用以下命令查看是否有任何PG进程在运行.对我来说没有,我甚至无法启动PG服务器:

    ps auxw | grep post
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我搜索了上面的错误消息中的.s.PGSQL.5432文件.我使用以下命令:

    sudo find / -name .s.PGSQL.5432 -ls
    
    Run Code Online (Sandbox Code Playgroud)

    在搜索我的整台计算机之后没有显示任何内容,因此文件不存在,但显然psql"想要它"或"认为它在那里".

  3. 我看了一下我的服务器日志,看到了以下错误:

    cat /usr/local/var/postgres/server.log
    
    Run Code Online (Sandbox Code Playgroud)

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

    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".
    
    Run Code Online (Sandbox Code Playgroud)
  4. 根据错误消息中的建议,我删除了与server.log相同的目录中的postmaster.pid文件.这解决了问题,我能够重新启动.

因此,似乎我的macbook冻结并且被重新启动导致Postgres认为它的进程即使在重启后仍然在运行.删除此文件已解决.希望这有助于他人!很多人都有类似的问题,但大多数答案都与文件权限有关,而在我的情况下,情况则有所不同.

  • 很好的答案.感谢您发布整个过程,而不仅仅是潜在的破坏性命令! (20认同)
  • 希望这个答案最终会帮助其他人,并感谢那个赞成其他人的人.尽管主持人将此问题标记为重复,但它是独特且有用的.感谢堆栈溢出为令人敬畏的网站和这里的专家分享! (8认同)
  • 我在macOS上的postgres登录位于`/ usr / local / var / log / postgres.log`。 (5认同)
  • 同意.关于postmaster.pid的答案很好,但是确定postgres是否正在运行的过程的每一步都是*非常宝贵*.谢谢! (3认同)
  • 只是FWIW,删除postmaster.pid是危险的,你必须*确实*确保没有'postgres`进程在你运行之前运行. (2认同)

ypi*_*ard 136

以上都不适合我.我不得不以下列方式重新安装Postgres:

  • 使用brew卸载postgresql: brew uninstall postgresql
  • brew doctor (修复这里的任何东西)
  • brew cleanup
  • 删除所有Postgres文件夹:

    • rm -r /usr/local/var/postgres
    • rm -r /Users/<username>/Library/Application\ Support/Postgres
  • 使用brew重新安装postgresql: brew install postgresql

  • 启动服务器: brew services start postgresql
  • 您现在应该创建数据库...(createdb)

  • 这个和最佳答案都不适合我,直到我还运行了 `brew postgresql-upgrade-database` (5认同)
  • 为我工作,但我不必删除实际的数据库,并且在应用程序支持中没有文件夹。我认为关键是一些 brew 元素在安装 postgres 时破坏了符号链接。修复它们并再次安装 postgres 解决了这个问题。谢谢! (4认同)

Jag*_*ari 77

如果你在macOS上并通过自制软件安装了postgres,请尝试重新启动它

brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Ubuntu,则可以使用其中一个命令重新启动它

sudo service postgresql restart

sudo /etc/init.d/postgresql restart
Run Code Online (Sandbox Code Playgroud)

  • 我重新启动了postgres,但仍然遇到相同的错误。 (3认同)

sma*_*-dm 28

这是我的方法:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm /usr/local/var/postgres/postmaster.pid
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Run Code Online (Sandbox Code Playgroud)

  • 类似但不同:) ...brew 服务停止 postgresql; rm /usr/local/var/postgres/postmaster.pid; 启动 postgresql 服务 (2认同)

Cra*_*ney 22

也许这是不相关的,但是当您使用升级postgres到主要版本时会出现类似的错误brew; 使用brew info postgresql发现这有助于:

To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
Run Code Online (Sandbox Code Playgroud)

  • 这对我在 Catalina 上从 11 点到 12 点很有用 (4认同)

Sat*_*wat 12

以下命令帮助了我。问题出在 PostgreSQL 数据版本上。升级后,它开始对我正常工作。

brew upgrade postgresql
brew postgresql-upgrade-database
brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)


Ant*_*ine 11

如果你postmaster.pid已经离开而你无法重启或任何事情,请执行以下操作:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

作为解释这里开始


Jac*_*lla 9

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

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

tail -n 10000 /usr/local/var/log/postgres.log
Run Code Online (Sandbox Code Playgroud)

并看到以下条目:

DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.
Run Code Online (Sandbox Code Playgroud)

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

brew postgresql-upgrade-database
Run Code Online (Sandbox Code Playgroud)

(奇怪的是,它在运行 1 时失败并在运行 2 中运行,所以在放弃之前尝试两次)


cra*_*aka 8

你好世界:)
对我来说最好但最奇怪的方法是做下一件事.

1)下载postgres93.app或其他版本.将此应用添加到/ Applications /文件夹中.

2)在文件中添加一行(命令).bash_profile(在我的主目录中):

export PATH=/Applications/Postgres93.app/Contents/MacOS/bin/:$PATH
这是一个路径,psqlPostgres93.app.每次启动控制台时都会运行行(命令).

3)Postgres93.app/Applications/文件夹启动.它启动本地服务器(端口为"5432",主机为"localhost").

4)经过所有这些操作后,我很高兴运行$ createuser -SRDP user_name和其他命令,并看到它的工作!Postgres93.app可以在每次系统启动时运行.

5)此外,如果您想以图形方式查看数据库,则应安装PG Commander.app.将postgres数据库视为漂亮的数据表是一种很好的方式

当然,它仅对本地服务器有用.如果这些指示可以帮助那些面临这个问题的人,我将很高兴.


Jer*_*ome 7

这个问题有很多来源,因而有很多答案.我经历过他们中的每一个.

1)如果你遇到某种崩溃,可能需要删除/usr/local/var/postgres/postmaster.pid文件,因为postgres可能没有正确处理它.但要确保没有进程正在运行.

2)Craig Ringer在其他帖子中指出Apple捆绑postgreSQL会导致pg gem安装问题 设置PATH环境变量是一种解决方案.

3)另一种解决方案是卸载并重新安装gem.Brew更新可能也是必要的.

如果您偶然发现这篇文章,如果您能确定其中一个来源,您将节省时间......

  • 我很高兴我发布了这个.另一次崩溃和答案可以很快恢复...... (3认同)

Mat*_*eux 6

对我来说,解决方案只是重启我的电脑.我首先尝试使用Brew服务重新启动,当它不起作用时,重新启动似乎是下一个最好的选择,在研究一些更复杂的解决方案之前.一切都按原样运作.


Jag*_*ari 5

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

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

sudo /etc/init.d/postgres status
Run Code Online (Sandbox Code Playgroud)

它会说这个过程已经死了,只要开始这个过程

sudo /etc/init.d/postgres start
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

165856 次

最近记录:

6 年,8 月 前