Postgres不允许localhost但与127.0.0.1一起使用

Sat*_*ish 12 sql linux postgresql connection

如果我说,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=#
Run Code Online (Sandbox Code Playgroud)

我的 /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
Run Code Online (Sandbox Code Playgroud)

如果我添加以下行,则failed启动Postgres服务:

host    all         all        localhost             ident
host    all         all        localhost             trust
Run Code Online (Sandbox Code Playgroud)

那有什么不对?

更新

我的/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
Run Code Online (Sandbox Code Playgroud)

Cas*_*Cas 7

问题

在指定以上指定的条件时,Postgres可能会使用IPv6,将返回密码提示。-h localhostpg_hba.confident

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


答案

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

# IPv6 local connections:
host    all         all         ::1/128               trust
Run Code Online (Sandbox Code Playgroud)

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


Erw*_*ter 6

在pg_hba.conf中,第一个匹配计数.每个文件:

具有匹配的连接类型,客户端地址,请求的数据库和用户名的第一个记录用于执行身份验证.没有"直通"或"备份":如果选择了一条记录且认证失败,则不考虑后续记录.如果没有记录匹配,则拒绝访问.

请注意相反的顺序:

host    all         all         127.0.0.1/32          trust
host    all         all         127.0.0.1/32          ident
Run Code Online (Sandbox Code Playgroud)

但:

host    all         all        localhost             ident
host    all         all        localhost             trust
Run Code Online (Sandbox Code Playgroud)

好吧,如果你真的"添加"你写的那些行,那么根本就没有任何影响.但如果你更换线路,那就有.

在第一种情况下,您将获得trust身份验证方法,这是一个门户开放策略.每个文件:

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

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

如果您实际使用的是过时的版本8.4,请转到8.4的旧手册.您知道8.4已经在2014年达到EOL并且不再受支持了吗?考虑升级到当前版本.

更多:

  • 天啊!!当我更改为“托管所有 127.0.0.1/32 md5”时它的工作原理如何???? (2认同)