为什么psql无法连接到服务器? [英] Why psql can't connect to server?
问题描述
我键入 psql
并得到以下信息:
I typed psql
and I get this:
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"?
我使用了 sudo netstat -nlp | grep 5432
查看状态,但未显示任何内容。
然后我在网上搜索,有人告诉我修改 pg_hba.conf
,但是我无法定位
文件。我还尝试了此命令 sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
。
I used sudo netstat -nlp | grep 5432
to see the status but nothing showed.
And I searched online, somebody told me to modify pg_hba.conf
but I can't locate
this file. And I also tried this commandsudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
. It can't work.
推荐答案
该错误表明psql实用程序找不到套接字来连接到数据库服务器。您要么没有在后台运行数据库服务,要么套接字位于其他位置,或者可能需要修复 pg_hba.conf
。
The error states that the psql utility can't find the socket to connect to your database server. Either you don't have the database service running in the background, or the socket is located elsewhere, or perhaps the pg_hba.conf
needs to be fixed.
该命令可能会因操作系统而异。但是,在大多数* ix系统上,下面的方法将起作用,它将在所有正在运行的进程中搜索postgres
The command may vary depending on your operating system. But on most *ix systems the following would work, it will search for postgres among all running processes
ps -ef | grep postgres
在我的系统上,mac osx会弹出
On my system, mac osx, this spits out
501 408 1 0 2Jul15 ?? 0:21.63 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
最后一列显示用于启动服务器的命令和选项。
The last column shows the command used to start the server, and the options.
您可以使用以下命令查看所有可用于启动postgres服务器的选项。
You can look at all the options available to start the postgres server using the following.
man postgres
从那里,您会看到选项 -D
和 -r
分别是 datadir
& 日志文件名
。
From there, you'd see that the options -D
and -r
are respectively the datadir
& the logfilename
.
使用查找
搜索套接字的位置,该位置应该在 / tmp
Use find
to search for the location of the socket, which should be somewhere in the /tmp
sudo find /tmp/ -name .s.PGSQL.5432
如果postgres正在运行并接受套接字连接,则上面的内容应该告诉您套接字的位置。在我的机器上,结果是:
If postgres is running and accepting socket connections, the above should tell you the location of the socket. On my machine, it turned out to be:
/tmp/.s.PGSQL.5432
然后,尝试使用该文件的位置明确地通过psql连接,例如。
Then, try connecting via psql using this file's location explicitly, eg.
psql -h /tmp/ dbname
步骤3:服务正在运行,但是您没有看到套接字
如果找不到套接字,但是看到服务正在运行,请验证pg_hba.conf文件允许本地套接字。
Step 3: If the service is running but you don't see a socket
If you can't find the socket, but see that the service is running, Verify that the pg_hba.conf file allows local sockets.
浏览到 datadir
,您应该找到 pg_hba。 conf
文件。
Browse to the datadir
and you should find the pg_hba.conf
file.
默认情况下,在文件底部附近,您会看到以下几行:
By default, near the bottom of the file you should see the following lines:
# "local" is for Unix domain socket connections only
local all all trust
如果没有看到,可以修改文件,然后重新启动postgres服务。
If you don't see it, you can modify the file, and restart the postgres service.
这篇关于为什么psql无法连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!