Postgres不允许本地主机,但与127.0.0.1

Postgres不接受连接,如果我说-h localhost但它的作品,如果我说-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服务failed启动:

 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 

但:

 host all all localhost ident host all all localhost trust 

那么,如果你真的“添加”你写的这样的句子,根本就没有任何效果。 但是,如果你更换线,那就是。

在第一种情况下,你会得到trust认证方法,这是一个开放的政策。 每个文档:

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

但是在第二种情况下,您将获得ident验证方法 ,该方法必须正确设置才能正常工作。

如果您实际使用的是过时版本8.4,请参阅旧手册8.4 。 你知道8.4在2014年已经达到EOL,不再被支持吗? 考虑升级到当前版本。

更多:

  • 使用psql命令不带密码运行批处理文件

问题

当指定-h localhost时,Postgres可能会使用IPv6,如果给定上述pg_hba.conf指定了ident ,则会返回密码提示。

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


答案

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

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

记住在进行配置更改后重新启动Postgres服务。