【问题标题】:Postgres not allowing localhost but works with 127.0.0.1Postgres 不允许 localhost 但可与 127.0.0.1 一起使用
【发布时间】:2015-06-30 09:34:12
【问题描述】:

如果我说-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 服务 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

【问题讨论】:

  • 我的猜测:localhost 未定义为 127.0.0.1 的网络别名。我在您的配置文件中看到“本地”,但没有看到“本地主机”。
  • 你也可以检查你的 /etc/host 的环回地址吗?
  • 127.0.0.1 localhost 中有/etc/hosts 我也更新了我的问题
  • 显然您必须提供您的 Postgres 版本。它像你的 psql 一样是 8.4 吗?顺便说一句,“Postgress”不是一个有效的名称。

标签: sql linux postgresql connection


【解决方案1】:

在 pg_hba.conf 中,第一个匹配项很重要。 The manual:

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

注意颠倒的顺序

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

记得在将更改保存到pg_hba.conf重新加载。 (不需要重启。)The manual:

pg_hba.conf 文件在启动时和主服务器时被读取 进程收到SIGHUP 信号。如果您在一个活动的 系统,您需要向邮局局长发出信号(使用pg_ctl reload, 调用 SQL 函数pg_reload_conf(),或使用kill -HUP) 让它重新读取文件。

如果你真的像你写的那样“添加”这些行,那么根本不应该有任何效果。但是,如果您替换这些行,就会有。

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

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

但在第二种情况下,您会得到ident authentication method,它必须正确设置才能工作。

另外,as Cas pointed out laterlocalhost 涵盖 IPv4 和 IPv6,而127.0.0.1/32 仅适用于 IPv4。

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

在 Postgres 9.1 或更高版本中,您宁愿使用 peer 而不是 ident

更多:

【讨论】:

  • 它是 8.4 现在我已经升级到 9.4 并且现在出现错误 psql: FATAL: Ident authentication failed for user "postgres" 我已将 ident 放在 pg_hba.conf 文件的首位。我也更改了postgres 密码,但不允许我进入。
  • 天啊!!当我更改为host all all 127.0.0.1/32 md5 时它的工作原理如何????
  • @Satish:因为md5 是一种完全不同的身份验证方法。阅读一下手册,我提供了链接。在 Postgres 9.1 或更高版本中,您也宁愿使用 peer 而不是 ident
【解决方案2】:

问题

Postgres 在指定-h localhost 时可能会使用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 服务。

【讨论】:

    猜你喜欢
    • 2011-08-25
    • 2017-05-25
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2011-06-07
    相关资源
    最近更新 更多