PostgreSQL:为什么 psql 无法连接到服务器? [英] PostgreSQL: Why psql can't connect to server?

查看:167
本文介绍了PostgreSQL:为什么 psql 无法连接到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入了 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 command sudo 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.

使用find查找socket的位置,应该在/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.

这篇关于PostgreSQL:为什么 psql 无法连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆