简体   繁体   English

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

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

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 Rails 3.2 applications.像其他一些人一样,当我在我的项目中运行 rake db:migrate 或什至为我的Ruby on Rails 3.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 PostgreSQL with Homebrew a long time ago and following an attempted installation of MongoDB recently my PostgreSQL install has never been the same.很久以前我用Homebrew安装了PostgreSQL ,最近尝试安装MongoDB之后,我的 PostgreSQL 安装从未如此。 I'm running OS X v10.6 Snow Leopard.我正在运行 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/LaunchAgents includes two PostgreSQL .plist files. PS: ~/Library/LaunchAgents包含两个 PostgreSQL .plist 文件。 I am not sure if that's relevant.我不确定这是否相关。

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.因为 OS X 安装了它自己的 PostgreSQL 版本,而 Homebrew 在不同的地方安装了不同的版本,并且 PostgreSQL 命令在 /tmp/ 目录中查找,所以我已经阅读过。 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.您需要在 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 .这是我发现更多尝试的链接,特别是按照上面的符号链接, Mac OSX Lion Postgres 不接受 /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.我仍然希望有人能对在 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,据我所知,这会导致大多数这些问题。

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/lunchy is a great wrapper for launchctl.检查 - https://github.com/mperham/lunchy是一个很好的 launchctl 包装器。

对我来说,这行得通

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

" 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. sudo rm -r pgsql_socket
  4. sudo ln -s /tmp pgsql_socket
  5. chown _postgres:_postgres pgsql_socket
  6. Restart PostgreSQL (not your computer)重启 PostgreSQL(不是你的电脑)

More information is available at " postgresql 9.0.3. on Lion Dev Preview 1 ".更多信息可在“ postgresql 9.0.3. on Lion Dev Preview 1 ”中找到。

This worked for me for the error you mentioned.对于您提到的错误,这对我有用。 Do one of these:执行以下操作之一

  1. Possibly change your default port specified in the postgres.conf file, if you have used something other than the default port 5432 while installing.如果您在安装时使用了默认端口5432以外的其他端口,则可以更改您在postgres.conf文件中指定的默认端口。

  2. Change the port number in postgresql.conf and restart the DB server.更改postgresql.conf中的端口号并重新启动数据库服务器。

  3. Instead of psql type the full command:代替psql输入完整的命令:

     psql -p 5432 -h localhost
    • server name and the port no服务器名称和端口号

If you are running Homebrew, uninstall Postgresql end pg gems:*如果您正在运行 Homebrew,请卸载 Postgresql end pg gem:*

$ 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我希望有帮助

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

I had that problem when I upgraded Postgres to 9.3.x.当我将 Postgres 升级到 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).对我来说,快速解决方法是降级到我之前拥有的任何 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. Homebrew install specific version of formula? ” 提供了更全面的解释以及解决问题的替代方法。

I encountered this error when running on Mac 10.15.5 using homebrew and seems to be a more updated solution that works where the ones above did not.我在使用自制软件在 Mac 10.15.5 上运行时遇到了这个错误,这似乎是一个更新的解决方案,适用于上述解决方案。

There is a file called postmaster.pid which should is automatically deleted when postresql exits.有一个名为 postmaster.pid 的文件应该在 postresql 退出时自动删除。

If it doesn't do the following如果它不执行以下操作

  • brew services stop postgresql <--- Failing to exit before rm could corrupt db brew services stop postgresql <--- 在 rm 之前无法退出可能会损坏数据库
  • sudo rm /usr/local/var/postgres/postmaster.pid sudo rm /usr/local/var/postgres/postmaster.pid

So for a lot of the issues here, it seems that people were already running psql and had to remove postmaster.pid .所以对于这里的很多问题,似乎人们已经在运行 psql 并且不得不删除postmaster.pid However, I did not have that issue as I never even had postgres installed in my system properly.但是,我没有这个问题,因为我什至从来没有在我的系统中正确安装过 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.我去了http://postgresapp.com/并下载了该应用程序。
  2. I moved the app to Application/ directory我将应用程序移至 Application/ 目录
  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我通过将其添加到 .bashrc 或 .bash_profile 或 .zshrc 将其添加到 $PATH : 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.我从 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

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.当您运行: psql -p 5432 -h localhost 它不使用环回驱动程序,而是使用真正的套接字,因此您需要配置 postgres 以接受套接字连接。 Hence even though pgadmin and other clients may work, psql will not.因此,即使 pgadmin 和其他客户端可以工作,psql 也不会。

You need to change both the postgresql.conf file and the pg_hba.conf files so allow connection over a real socket.您需要同时更改 postgresql.conf 文件和 pg_hba.conf 文件,以便允许通过真实套接字进行连接。 This will also allow connection from a remote ip which is why it is disabled by default.这也将允许从远程 IP 连接,这就是默认禁用它的原因。

These files are in the data directory but the actual location can be different depending on how postgres was installed.这些文件位于数据目录中,但实际位置可能会有所不同,具体取决于 postgres 的安装方式。 With postgres running, run the command:运行 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

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.您想弄清楚试图启动 PostgreSQL 的日志在哪里,以及 PostgreSQL 的套接字目录是什么。 By default when you build PG, the socket directory is /tmp/.默认情况下,当你构建 PG 时,socket 目录是 /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如果您在构建 PG 并启动 PG 时没有更改它,那么您应该能够在 /tmp 中看到一个套接字文件:ls -al /tmp

The socket file starts with a ".", so you won't see it with the '-a' to ls.套接字文件以“.”开头,因此您不会看到它带有 ls 的“-a”。

If you don't see a socket there, and you don't see anything from ps awux |如果您在那里看不到套接字,并且您在 ps awux | 中看不到任何东西grep postgres, then PG is probably not running, or maybe it is and it's the OSX-installed one. grep postgres,然后 PG 可能没有运行,或者它可能是并且它是 OSX 安装的。 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.可能发生的情况是,您可能在 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.我听说自制软件会让事情变得有点难看,我交谈过的很多人都鼓励使用虚拟机。

for me command rm /usr/local/var/postgres/postmaster.pid didn't work cause I installed a specific version of postgresql with homebrew.对我来说,命令rm /usr/local/var/postgres/postmaster.pid不起作用,因为我用自制软件安装了特定版本的 postgresql。

the right command is rm /usr/local/var/postgres@10/postmaster.pid .正确的命令是rm /usr/local/var/postgres@10/postmaster.pid

then brew services restart postgresql@10 .然后brew services restart postgresql@10

The Cause原因

Lion comes with a version of postgres already installed and uses those binaries by default. Lion 已经安装了一个 postgres 版本,并默认使用这些二进制文件。 In general you can get around this by using the full path to the homebrew postgres binaries but there may be still issues with other programs.通常,您可以通过使用自制 postgres 二进制文件的完整路径来解决此问题,但其他程序可能仍然存在问题。

The Solution解决方案

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh |卷曲http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

Via通过

http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/ http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/

One reason you get this error is that your local postgres database shuts down when you restart your computer.您收到此错误的一个原因是您的本地 postgres 数据库在您重新启动计算机时关闭。 In a new terminal window, simply type:在新的终端窗口中,只需键入:

$psql -h localhost 

to restart the server.重新启动服务器。

Hello world :)你好世界 :)
The best but strange way for me was to do next things.对我来说最好但奇怪的方法是做接下来的事情。

1) Download postgres93.app or other version. 1)下载postgres93.app或其他版本。 Add this app into /Applications/ folder.将此应用程序添加到 /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
It's a PATH to psql from Postgres93.app . 这是从Postgres93.apppsql的路径。 The row (command) runs every time console is started. 每次启动控制台时都会运行该行(命令)。

3) Launch Postgres93.app from /Applications/ folder. 3)/Applications/文件夹启动Postgres93.app It starts a local server (port is "5432" and host is "localhost").它启动一个本地服务器(端口是“5432”,主机是“localhost”)。

4) After all of this manipulations I was glad to run $ createuser -SRDP user_name and other commands and to see that it worked! 4)在所有这些操作之后,我很高兴运行$ createuser -SRDP user_name和其他命令并看到它有效! Postgres93.app can be made to run every time your system starts.每次系统启动时都可以运行Postgres93.app

5) Also if you wanna see your databases graphically you should install PG Commander.app . 5)此外,如果您想以图形方式查看数据库,您应该安装PG Commander.app It's good way to see your postgres DB as pretty data-tables将您的 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.如果此说明对遇到此问题的其他人有所帮助,我将很高兴。

I had PostgreSQL 9.3 and got the same error,我有 PostgreSQL 9.3 并得到了同样的错误,

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?无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?

I fixed this using:我使用以下方法解决了这个问题:

chmod 777  /var/lib/pgsql/9.3/data/pg_hba.conf 
service postgresql-9.3 restart

It works for me.这个对我有用。

for what it's worth, I experienced this same error when I had a typo ( md4 instead of md5 ) in my pg_hba.conf file ( /etc/postgresql/9.5/main/pg_hba.conf )对于它的价值,当我的pg_hba.conf文件( /etc/postgresql/9.5/main/pg_hba.conf )中有错字( md4而不是md5 )时,我遇到了同样的错误

If you got here as I did, double-check that file once to make sure there isn't anything ridiculous in there.如果您像我一样来到这里,请仔细检查该文件一次,以确保其中没有任何可笑的东西。

只要通过运行brew services restart postgresql出现错误就重新启动 Postgres,然后再试一次

There might be different issues with running PostgreSQL locally.在本地运行 PostgreSQL 可能会有不同的问题。 I would recommend to clean all versions of postgres installed and start fresh.我建议清理所有已安装的 postgres 版本并重新开始。 Once you have it installed, it's very easy to recreate your database if you have your rails project with the up to date db/schema.rb安装后,如果您的 rails 项目使用最新的db/schema.rb ,那么重新创建数据库非常容易

Here is how I usually install PostgreSQL on a Mac .这是我通常在 Mac 上安装 PostgreSQL 的方法。 If you're running your database under the root user locally, you might want to omit the last step that creates a todo user如果您在本地 root 用户下运行数据库,您可能希望省略创建todo用户的最后一步

One of the reason could be that原因之一可能是

unix_socket_directories in postgresql.conf file is not listed with the directory it is looking for. postgresql.conf 文件中的 unix_socket_directories 未与它要查找的目录一起列出。

In the question example it is looking for directory /tmp this has to be provided in the postgresql.conf file在问题示例中,它正在寻找目录 /tmp 这必须在 postgresql.conf 文件中提供

something like this:像这样的东西:

unix_socket_directories = '/var/run/postgresql,/tmp'    # comma-separated list of directories

This solution worked for me.这个解决方案对我有用。

If you're on MacOS and using homebrew I found this answer extremely helpful:如果您使用的是 MacOS 并使用自制软件,我发现这个答案非常有帮助:

https://stackoverflow.com/a/27708774/4062901 https://stackoverflow.com/a/27708774/4062901

For me, my server was running but because of an upgrade I was having issues connecting (note: I use brew services).对我来说,我的服务器正在运行,但由于升级,我遇到了连接问题(注意:我使用 brew 服务)。 If Postgres is running, try cat /usr/local/var/postgres/server.log and see what the logs say.如果 Postgres 正在运行,请尝试cat /usr/local/var/postgres/server.log并查看日志内容。 My error was a migration, not a connection issue.我的错误是迁移,而不是连接问题。

Also after the manual migration they propose (which does work but) I found there was no longer a table for my user.同样在他们提出的手动迁移之后(确实有效,但是)我发现我的用户不再有一个表。 Try this command to fix that: createdb (answered via psql: FATAL: database "<user>" does not exist )试试这个命令来解决这个问题: createdb (通过psql 回答: FATAL: database "<user>" does not exist

The following solution from a different stackoverflow question helped me:来自不同stackoverflow问题的以下解决方案帮助了我:

Find the postgres data directory.找到 postgres 数据目录。 On a MAC using homebrew it is /usr/local/var/postgres/ , other systems it might be /usr/var/postgres/在使用自制软件的 MAC 上,它是/usr/local/var/postgres/ ,其他系统可能是/usr/var/postgres/

Remove pid file by running:通过运行删除 pid 文件:

rm postmaster.pid

Restart postgress.重新启动postgress。 On Mac, run:在 Mac 上,运行:

brew services restart postgresql

It's very simple.这很简单。 Only add host in your database.yaml file.仅在您的 database.yaml 文件中添加主机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL无法连接到服务器-没有这样的文件或目录 - postgresql could not connect to server - No such file or directory Postgresql 版本 13 psql:错误:无法连接到服务器:无法连接到服务器:没有这样的文件或目录 - Postgresql version 13 psql: error: could not connect to server: could not connect to server: No such file or directory PostgreSQL:无法连接到服务器:没有这样的文件或目录 - Postgresql: could not connect to server: No such file or directory PostgreSQL 无法连接到服务器:没有这样的文件或目录 - PostgreSQL could not connect to server: No such file or directory PostgreSQL 错误:无法连接到数据库模板 1:无法连接到服务器:没有这样的文件或目录 - PostgreSQL error: could not connect to database template1: could not connect to server: No such file or directory PostgreSQL无法连接:处理请求时发生意外错误:无法连接到服务器:没有此类文件或目录 - PostgreSQL could not connect to: Unexpected error while processing request: could not connect to server: No such file or directory psql:错误:无法连接到服务器:没有这样的文件或目录 - psql: error: could not connect to server: No such file or directory PostgreSQL:'psql:错误:无法连接到服务器:没有这样的文件或目录'.s.PGSQL.5432 - PostgreSQL : 'psql: error: could not connect to server: No such file or directory' .s.PGSQL.5432 Travis-CI 和 PostgreSQL:psql:错误:无法连接到服务器:没有这样的文件或目录 - Travis-CI and PostgreSQL: psql: error: could not connect to server: No such file or directory 第一个ruby和postgreSql应用程序 - 获取无法连接到服务器:没有这样的文件或目录 - First ruby and postgreSql application - getting could not connect to server: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM