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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13573204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:51:48  来源:igfitidea点击:

psql: could not connect to server: No such file or directory (Mac OS X)

macospostgresql

提问by FireDragon

Upon restarting my Mac I got the dreaded Postgres error:

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

The reason this happened is because my macbook froze completely due to an unrelated issue and I had to do a hard reboot using the power button. After rebooting I couldn't start Postgres because of this error.

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

回答by FireDragon

WARNING: If you delete postmaster.pid without making sure there are really no postgresprocesses running you, could permanently corrupt your database. (PostgreSQL should delete it automatically if the postmaster has exited.).

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

SOLUTION: This fixed the issue--I deleted this file, and then everything worked!

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

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

--

——

and here is how I figured out why this needed to be deleted.

这就是我如何弄清楚为什么需要删除它。

  1. I used the following command to see if there were any PG processes running. for me there were none, I couldn't even start the PG server:

    ps auxw | grep post
    
  2. I searched for the file .s.PGSQL.5432 that was in the error message above. i used the following command:

    sudo find / -name .s.PGSQL.5432 -ls
    

    this didn't show anything after searching my whole computer so the file didn't exist, but obviously psql"wanted it to" or "thought it was there".

  3. I took a look at my server logs and saw the following error:

    cat /usr/local/var/postgres/server.log
    

    at the end of the server log I see the following error:

    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".
    
  4. Following the advice in the error message, I deleted the postmaster.pid file in the same directory as server.log. This resolved the issue and I was able to restart.

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

    ps auxw | grep post
    
  2. 我搜索了上面错误消息中的文件 .s.PGSQL.5432。我使用了以下命令:

    sudo find / -name .s.PGSQL.5432 -ls
    

    在搜索我的整个计算机后,这没有显示任何内容,因此该文件不存在,但显然psql“想要”或“认为它在那里”。

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

    cat /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".
    
  4. 按照错误消息中的建议,我删除了 server.log 所在目录中的 postmaster.pid 文件。这解决了问题,我能够重新启动。

So, it seems that my macbook freezing and being hard-rebooted caused Postgres to think that it's processes were still running even after reboot. Deleting this file resolved. Hope this helps others! Lots of people have similar issues but most the answers had to do with file permissions, whereas in my case things were different.

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

回答by ypicard

None of the above worked for me. I had to reinstall Postgres the following way :

以上都不适合我。我不得不通过以下方式重新安装 Postgres:

  • Uninstall postgresql with brew : brew uninstall postgresql
  • brew doctor(fix whatever is here)
  • brew cleanup
  • Remove all Postgres folders :

    • rm -r /usr/local/var/postgres
    • rm -r /Users/<username>/Library/Application\ Support/Postgres
  • Reinstall postgresql with brew : brew install postgresql

  • Start server : brew services start postgresql
  • You should now have to create your databases... (createdb)
  • 使用 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)

回答by Jagdish Barabari

If you're on macOS and installed postgres via homebrew, try restarting it with

如果您使用的是 macOS 并通过自制软件安装了 postgres,请尝试使用以下命令重新启动它

brew services restart postgresql

If you're on Ubuntu, you can restart it with either one of these commands

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

sudo service postgresql restart

sudo /etc/init.d/postgresql restart

回答by Crazy Barney

Maybe this is unrelated but a similar error appears when you upgrade postgresto a major version using brew; using brew info postgresqlfound out this that helped:

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

To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database

回答by smartworld-dm

Here is my way:

这是我的方法:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm /usr/local/var/postgres/postmaster.pid
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

回答by Antoine

if your postmaster.pidis gone and you can't restart or anything, do this:

如果您postmaster.pid已经离开并且您无法重新启动或任何事情,请执行以下操作:

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

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

as explained hereinitially

正如这里最初解释的

回答by crazzyaka

Hello world :)
The best but strange way for me was to do next things.

1)Download postgres93.appor other version. Add this app into /Applications/ folder.

Hello world :)
对我来说最好但很奇怪的方法是做接下来的事情。

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

2)Add a row (command) into the file .bash_profile(which is in my home directory):

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

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

3)Launch Postgres93.appfrom /Applications/folder. It starts a local server (port is "5432" and host is "localhost").

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

4)After all of this manipulations I was glad to run $ createuser -SRDP user_nameand other commands and to see that it worked! Postgres93.appcan be made to run every time your system starts.

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

5)Also if you wanna see your databases graphically you should install PG Commander.app. It's good way to see your postgres DB as pretty data-tables

5)另外,如果你想以图形方式查看你的数据库,你应该安装PG Commander.app. 将您的 postgres 数据库视为漂亮的数据表的好方法

Of, course, it's helpful only for local server. I will be glad if this instructions help others who has faced with this problem.

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

回答by Jerome

This problema has many sources, and thus many answers. I've experienced each one of them.

这个问题有很多来源,因此也有很多答案。我经历了他们每一个人。

1) If you have a crash of some sort, removing the /usr/local/var/postgres/postmaster.pid file is probably required as postgres may not have handled it properly. But ensure that no process is running.

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

2) Craig Ringer has pointed out in other posts that Apple's bundling of postgreSQL leads to pg gem installation issuesSetting the PATH environment variable is a solution.

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

3) Another solution, is to uninstall and reinstall the gem. A brew update may be necessary as well.

3)另一种解决方案是卸载并重新安装gem。也可能需要 brew 更新。

If you stumble upon this post, if you can pinpoint one of the sources, you'll save time...

如果你偶然发现这篇文章,如果你能查明其中一个来源,你会节省时间......

回答by Matt Lemieux

For me, the solution was simply restart my computer. I first tried restarting with Brew services and when that didn't work, restarting seemed like the next best option to try before looking into some of the more involved solutions. Everything worked as it should after.

对我来说,解决方案只是重新启动我的电脑。我首先尝试使用 Brew 服务重新启动,当这不起作用时,重新启动似乎是在研究一些更复杂的解决方案之前尝试的下一个最佳选择。之后一切正常。

回答by Jagdish Barabari

I was facing a similar issue here I solved this issue as below.

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

Actually the postgres process is dead, to see the status of postgres run the following command

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

sudo /etc/init.d/postgres status

It will says the process is dead`just start the process

它会说进程已经死了`just start the process

sudo /etc/init.d/postgres start