Sql Postgres不允许本地主机,但可与127.0.0.1一起使用

Sql Postgres不允许本地主机,但可与127.0.0.1一起使用,sql,linux,postgresql,connection,Sql,Linux,Postgresql,Connection,如果我说-h localhost,Postgres不接受连接,但如果我说-h 127.0.0.1 [root@5d9ca0effd7f opensips]# psql -U postgres -h localhost -W Password for user postgres: psql: FATAL: Ident authentication failed for user "postgres" [root@5d9ca0effd7f opensips]# psql -U

如果我说
-h localhost
,Postgres不接受连接,但如果我说
-h 127.0.0.1

[root@5d9ca0effd7f opensips]# psql -U postgres -h localhost -W
Password for user postgres:
psql: FATAL:  Ident authentication failed for user "postgres"
[root@5d9ca0effd7f opensips]# psql -U postgres -h 127.0.0.1 -W
Password for user postgres:
psql (8.4.20)
Type "help" for help.

postgres=#
我的
/var/lib/pgsql/data/pg_hba.conf

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                              trust
local   all         all                              ident
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
host    all         all         127.0.0.1/32          ident
# IPv6 local connections:
host    all         all         ::1/128               ident
如果我添加以下行,则Postgres服务
无法启动:

host    all         all        localhost             ident
host    all         all        localhost             trust
怎么了

更新 我的
/etc/hosts
文件:

[root@5d9ca0effd7f opensips]# cat /etc/hosts
172.17.0.2      5d9ca0effd7f
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

在pg_hba.conf中,第一次匹配计数

具有匹配连接类型、客户端地址、, 请求的数据库和用户名用于执行身份验证。 没有“失败”或“备份”:如果选择了一条记录 身份验证失败,不考虑后续记录。如果没有 记录匹配,访问被拒绝

请注意顺序相反的

host    all         all         127.0.0.1/32          trust
host    all         all         127.0.0.1/32          ident
但是:

将更改保存到
pg_hba.conf
后,请记住重新加载。(无需重新启动。)

启动时和主服务器启动时读取
pg_hba.conf
文件 进程接收一个SIGHUP信号。如果您在活动服务器上编辑文件 系统中,您需要向邮政局长发出信号(使用
pg\u ctl reload
, 调用SQL函数
pg_reload_conf()
,或使用
kill-HUP
)来 让它重新读取文件

如果你真的像你写的那样“添加”了这些行,那么应该不会有任何效果。但是,如果您替换行,则会出现错误

在第一种情况下,您将获得
trust
身份验证方法,这是一种开放式策略

PostgreSQL假定可以连接到服务器的任何人都是 授权以任何数据库用户名访问数据库 它们指定(甚至是超级用户名)

但是,在第二种情况下,您得到的是,必须正确设置才能工作

另外,
localhost
同时涵盖IPv4和IPv6,而
127.0.0.1/32
仅适用于IPv4。如果使用IPv6,这是一个重要的区别

如果您实际使用的是过时的8.4版,请转到。您知道并且不再受支持吗?考虑升级到当前版本。

在Postgres 9.1或更高版本中,您也更愿意使用
对等
而不是
标识

更多:

问题 Postgres在指定
-h localhost
时可能会使用IPv6,如果上述
pg_hba.conf
指定
ident
,将返回密码提示

但是,当指定
-h 127.0.0.1
时,它会强制Postgres使用IPv4,在上述配置中,IPv4设置为
trust
,并允许无密码访问


答案 因此,答案是将
pg_hba.conf
中的IPv6主机行修改为使用
trust

# IPv6 local connections:
host    all         all         ::1/128               trust

记得在更改配置后重新启动Postgres服务。

My guess:localhost没有定义为127.0.0.1的网络别名。我在你的配置文件中看到了“local”,但没有看到“localhost”。你能检查一下你的/etc/host的环回地址吗?它在
/etc/hosts
中有
127.0.0.1 localhost
,我也更新了我的问题。显然,你必须提供你的Postgres版本。它8.4和你的psql一样吗?顺便说一句,“postgres”不是一个有效的名称。它是
8.4
现在我已经升级到
9.4
,现在出现错误
psql:FATAL:Ident用户“postgres”身份验证失败
我已经将
Ident
放在
pg_hba.conf
文件的第一位。我还更改了postgres的密码,但不允许我进入。天哪!!当我改为
host all 127.0.0.1/32 md5
How???@Satish时,它会起作用:因为
md5
是一种完全不同的身份验证方法。阅读一下手册,我提供了链接。在Postgres 9.1或更高版本中,您也更愿意使用
peer
而不是
ident
# IPv6 local connections:
host    all         all         ::1/128               trust