PostgreSQL 错误“无法连接到服务器:没有这样的文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12472988/
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:43:28  来源:igfitidea点击:

PostgreSQL error 'Could not connect to server: No such file or directory'

postgresqlosx-snow-leopardhomebrew

提问by Evolve

Like some others I am getting this error when I run rake db:migrate in my project or even try most database tasks for my Ruby on Rails3.2 applications.

像其他一些人一样,当我在我的项目中运行 rake db:migrate 时,甚至尝试为我的Ruby on Rails3.2 应用程序执行大多数数据库任务时,我也会收到此错误。

PGError (could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

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

I installed PostgreSQLwith Homebrewa long time ago and following an attempted installation of MongoDBrecently my PostgreSQL install has never been the same. I'm running OS X v10.6 Snow Leopard.

我很久以前用Homebrew安装了PostgreSQL,最近尝试安装MongoDB 之后,我的 PostgreSQL 安装从未如此。我正在运行 OS X v10.6 Snow Leopard。

What's wrong and how do I better understand how PostgreSQL is and should be setup on my Mac?

出了什么问题,我如何更好地了解 PostgreSQL 是如何以及应该如何在我的 Mac 上设置的?

So far (I think) this tells me that PostgreSQL is not running(?).

到目前为止(我认为)这告诉我 PostgreSQL 没有运行(?)。

ps -aef|grep postgres                                                                                                   (ruby-1.9.2-p320@jct-ana) (develop) ?
  501 17604 11329   0   0:00.00 ttys001    0:00.00 grep postgres

But does this tell me that PostgreSQL is running?

但这是否告诉我 PostgreSQL 正在运行?

? launchctl load -w /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist                                                        (ruby-1.9.2-p136) 
homebrew.mxcl.postgresql: Already loaded

How do I fix this? what am I not seeing?

我该如何解决?我没有看到什么?

PS: ~/Library/LaunchAgentsincludes two PostgreSQL .plist files. I am not sure if that's relevant.

PS:~/Library/LaunchAgents包括两个 PostgreSQL .plist 文件。我不确定这是否相关。

org.postgresql.postgres.plist
homebrew.mxcl.postgresql.plist

I tried the following and got a result as below.

我尝试了以下并得到如下结果。

$ psql -p 5432 -h localhost

$ psql -p 5432 -h 本地主机

psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80::1) and accepting
    TCP/IP connections on port 5432?

I've read since that this is occuring because OS X installs its own version of PostgreSQL and Homebrew installs a different version in a different place and the PostgreSQL commands are looking in the /tmp/ directory. You'll need to search more on Stack Overflow, but basically you symlink PostgreSQL so that anything looking in that tmp path actually finds the real path, if that makes sense.

我读过这是因为 OS X 安装了自己的 PostgreSQL 版本,而 Homebrew 在不同的地方安装了不同的版本,并且 PostgreSQL 命令在 /tmp/ 目录中查找。您需要在 Stack Overflow 上搜索更多内容,但基本上您对 PostgreSQL 进行符号链接,以便在该 tmp 路径中查找的任何内容实际上都能找到真正的路径,如果这有意义的话。

This is the link where I found a few more things to try, specifically doing the symlink as per above, Mac OSX Lion Postgres does not accept connections on /tmp/.s.PGSQL.5432. I still wish someone would put together a decent explanation of the concepts behind installing PostgreSQL on OS X and why it's all so difficult.

这是我发现更多尝试的链接,特别是按照上述方式执行符号链接,Mac OSX Lion Postgres 不接受 /tmp/.s.PGSQL.5432 上的连接。我仍然希望有人能够对在 OS X 上安装 PostgreSQL 背后的概念以及为什么这一切如此困难做出一个体面的解释。

Latest insights to help with troubleshooting:

有助于排除故障的最新见解:

$ which psql // This tells you which PostgreSQL you are using when you run $ psql. 

Then run:

然后运行:

$ echo $PATH

The key thing to take into account is this:

要考虑的关键是:

Ensure that the path entry for the copy of PostgreSQL you want to run COMES BEFORE the path to the OS X system's PostgreSQL.

确保您要运行的 PostgreSQL 副本的路径条目位于 OS X 系统的 PostgreSQL 路径之前。

This is a core requirement which decides which PostgreSQL gets run and is what I'm told leads to most of these issues.

这是决定运行哪个 PostgreSQL 的核心要求,据我所知,这会导致大多数这些问题。

回答by Hass

Check there is no postmaster.pid in your postgres directory, probably /usr/local/var/postgres/

检查您的 postgres 目录中是否没有 postmaster.pid,可能是 /usr/local/var/postgres/

remove this and start server.

删除它并启动服务器。

Check - https://github.com/mperham/lunchyis a great wrapper for launchctl.

检查 - https://github.com/mperham/lunchy是 launchctl 的一个很好的包装器。

回答by josephmisiti

"Postgres.app" is a better fix if you are on OS X

如果您使用的是 OS X,“ Postgres.app” 是一个更好的解决方案



Here is the fix:

这是修复:

  1. Stop the database
  2. cd /var
  3. rm -r pgsql_socket
  4. ln -s /tmp pgsql_socket
  5. chown _postgres:_postgres pgsql_socket
  6. Restart PostgreSQL (not your computer)
  1. 停止数据库
  2. cd /var
  3. rm -r pgsql_socket
  4. ln -s /tmp pgsql_socket
  5. chown _postgres:_postgres pgsql_socket
  6. 重启 PostgreSQL(不是你的电脑)

More information is available at "postgresql 9.0.3. on Lion Dev Preview 1".

更多信息可在“ postgresql 9.0.3. on Lion Dev Preview 1”获得。

回答by tsunllly

For me, this works

对我来说,这有效

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

回答by solaimuruganv

This worked for me for the error you mentioned. Do oneof these:

对于您提到的错误,这对我有用。执行以下操作之一

  1. Possibly change your default port specified in the postgres.conffile, if you have used something other than the default port 5432while installing.

  2. Change the port number in postgresql.confand restart the DB server.

  3. Instead of psqltype the full command:

    psql -p 5432 -h localhost   
    
    • server name and the port no
  1. postgres.conf如果您5432在安装时使用了默认端口以外的其他端口,则可以更改文件中指定的默认端口 。

  2. 更改端口号postgresql.conf并重新启动数据库服务器。

  3. 而不是psql键入完整的命令:

    psql -p 5432 -h localhost   
    
    • 服务器名称和端口号

回答by Fabricio

If you are running Homebrew, uninstall Postgresql end pg gems:*

如果您正在运行 Homebrew,请卸载 Postgresql end pg gems:*

$ gem uninstall pg
$ brew uninstall postgresql

Download and run the following script to fix permission on /usr/local:*https://gist.github.com/rpavlik/768518

下载并运行以下脚本以修复 /usr/local 上的权限:* https://gist.github.com/rpavlik/768518

$ ruby fix_homebrew.rb

Then install Postgres again and pg gem:*

然后再次安装 Postgres 和 pg gem:*

$ brew install postgresql  
$ initdb /usr/local/var/postgres -E utf8

To have launchd start postgresql at login run:

要在登录时启动 postgresql,请运行:

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 

Or start manually.

或者手动启动。

Install pg gem

安装 pg gem

$ gem install pg

I hope have helped

我希望有帮助

回答by silvenon

I had that problem when I upgraded Postgres to 9.3.x. The quick fix for me was to downgrade to whichever 9.2.x version I had before (no need to install a new one).

当我将 Postgres 升级到 9.3.x 时,我遇到了这个问题。对我来说,快速修复是降级到我之前拥有的任何 9.2.x 版本(无需安装新版本)。

$ ls /usr/local/Cellar/postgresql/
9.2.4
9.3.2
$ brew switch postgresql 9.2.4
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
<open a new Terminal tab or window to reload>
$ psql

"Homebrew install specific version of formula?" offers a much more comprehensive explanation along with alternative ways to fix the problem.

自制安装特定版本的公式?”提供了更全面的解释以及解决问题的替代方法。

回答by Nelu

I fixed this by upgrading postgres.

我通过升级 postgres 解决了这个问题。

brew update
brew upgrade

Then I had to also upgrade the databases to be compatible with the new major version, because I upgraded from 10 to 11.

然后我还必须升级数据库以与新的主要版本兼容,因为我从 10 升级到 11。

brew postgresql-upgrade-database

(source https://github.com/facebook/react-native/issues/18760#issuecomment-410533581)

(来源https://github.com/facebook/react-native/issues/18760#issuecomment-410533581

回答by superjisan

So for a lot of the issues here, it seems that people were already running psql and had to remove postmaster.pid. However, I did not have that issue as I never even had postgres installed in my system properly.

所以对于这里的很多问题,似乎人们已经在运行 psql 并且不得不删除postmaster.pid. 但是,我没有这个问题,因为我什至从未在我的系统中正确安装过 postgres。

Here's a solution that worked for me in MAC OSX Yosemite

这是在 MAC OSX Yosemite 中对我有用的解决方案

  1. I went to http://postgresapp.com/and downloaded the app.
  2. I moved the app to Application/ directory
  3. I added it to $PATH by adding this to .bashrc or .bash_profile or .zshrc : export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
  4. I ran the postgres from the Applications directory and ran the command again and it worked.
  1. 我去了http://postgresapp.com/并下载了该应用程序。
  2. 我将应用程序移至 Application/ 目录
  3. 我通过将其添加到 .bashrc 或 .bash_profile 或 .zshrc 将其添加到 $PATH : export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
  4. 我从 Applications 目录运行 postgres 并再次运行该命令并且它起作用了。

Hope this helps! Toodles!

希望这可以帮助!玩具!

Also, this answer helped me the most: https://stackoverflow.com/a/21503349/3173748

此外,这个答案对我帮助最大:https: //stackoverflow.com/a/21503349/3173748

回答by oenpelli

When you run: psql -p 5432 -h localhost it does not use the loopback driver but uses a real socket and hence you need to configure postgres to accept socket connections. Hence even though pgadmin and other clients may work, psql will not.

当您运行: psql -p 5432 -h localhost 它不使用环回驱动程序而是使用真正的套接字,因此您需要配置 postgres 以接受套接字连接。因此,即使 pgadmin 和其他客户端可以工作,psql 也不会。

You need to change both the postgresql.conf file and the pg_hba.conf files so allow connection over a real socket. This will also allow connection from a remote ip which is why it is disabled by default.

您需要更改 postgresql.conf 文件和 pg_hba.conf 文件,以便允许通过真实套接字进行连接。这也将允许来自远程 ip 的连接,这就是默认情况下禁用它的原因。

These files are in the data directory but the actual location can be different depending on how postgres was installed. With postgres running, run the command:

这些文件位于数据目录中,但实际位置可能会有所不同,具体取决于 postgres 的安装方式。运行 postgres 后,运行以下命令:

ps -ef | grep postmaster

These two files are the -D directory (maybe /usr/local/pgsql/data).

这两个文件是-D目录(可能是/usr/local/pgsql/data)。

For the postgresql.conf file, uncomment the listen_address and change it to be:

对于 postgresql.conf 文件,取消对 listen_address 的注释并将其更改为:

listen_address = '*'

For the pg_hba.conf add the line:

对于 pg_hba.conf 添加以下行:

host all all 0.0.0.0/0 md5

回答by Stephen Frost

I don't really know Mac or Homebrew, but I know PostgreSQL very well.

我不太了解 Mac 或 Homebrew,但我非常了解 PostgreSQL。

You want to figure out where the logs are from PostgreSQL trying to start and what the socket directory is for PostgreSQL. By default when you build PG, the socket directory is /tmp/. If you didn't change that when you built PG and then you started PG, you should be able to see a socket file in /tmp if you do: ls -al /tmp

您想弄清楚 PostgreSQL 尝试启动的日志来自何处,以及 PostgreSQL 的套接字目录是什么。默认情况下,当您构建 PG 时,socket 目录是 /tmp/。如果您在构建 PG 然后启动 PG 时没有更改它,那么您应该能够在 /tmp 中看到一个套接字文件: ls -al /tmp

The socket file starts with a ".", so you won't see it with the '-a' to ls.

套接字文件以“.”开头,因此您不会看到带有 '-a' 到 ls 的文件。

If you don't see a socket there, and you don't see anything from ps awux | grep postgres, then PG is probably not running, or maybe it is and it's the OSX-installed one. What might be happening is that you might be getting a conflict on listening on port 5432 on localhost- use netstat -anp to see what, if anything, is listening on 5432. If a Mac OSX PG is already listening on that port then that might be the problem.

如果在那里没有看到套接字,并且在 ps awux 中没有看到任何内容 | grep postgres,那么 PG 可能没有运行,或者可能是并且它是 OSX 安装的。可能发生的情况是,您可能在侦听 localhost 上的端口 5432 时遇到冲突 - 使用 netstat -anp 查看正在侦听 5432 的内容(如果有的话)。如果 Mac OSX PG 已经在侦听该端口,则可能成为问题。

Hope that helps. I have heard that homebrew can make things a bit ugly and a lot of people I've talked to encourage using a VM instead.

希望有帮助。我听说自制软件会让事情变得有点难看,我与之交谈过的很多人都鼓励使用 VM。