【问题标题】:Unknown reason for postgres password authentication failurepostgres密码认证失败的未知原因
【发布时间】:2018-05-18 00:31:01
【问题描述】:

我无法登录和使用创建的数据库,因为我收到密码或对等身份验证失败。下面概述了我是如何设置的,我是如何尝试解决遇到的问题的,以及更多潜在的相关信息。

我正在使用postgresql-9.5,当我下载 postgres 时,它使用的 linux mint 用户/用户名与我目前尝试访问的不同。以下所有命令都是从新用户/用户名发送的。

我使用以下命令创建了一个新的 postgres 数据库和一个带有密码的新用户:sudo su - postgres

$ psql

postgres=# CREATE DATABASE PostgresDB;

postgres=# CREATE USER PostgresUser WITH PASSWORD 'password';

postgres=# ALTER ROLE PostgressUser SET default_transaction_isolation TO 'read committed'

postgres=# GRANT ALL PRIVILEGES ON DATABASE PostgresDB TO PostgresUser;

然后我继续尝试以多种方式登录:

  • psql -d PostgresDB -U PostgresUser

返回:psql: FATAL: Peer authentication failed for user "PostgresUser"

  • psql -d PostgresDB -U PostgresUser -W 和密码输入
  • psql -dpsql -U PostgresUser -h 127.0.0.1 -d PostgresDB 和密码输入

每个返回:psql: FATAL: password authentication failed for user "PostgresUser"

在重置密码之前和之后,我进行了上述所有尝试登录:ALTER PostgresUser WITH PASSWORD 'password'

我检查了 pg_hba.conf 文件,其中的重要部分如下:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

【问题讨论】:

    标签: postgresql ubuntu-16.04 linux-mint postgresql-9.5


    【解决方案1】:

    您是否尝试过查看日志文件?

    要解谜,试试这个:

    SELECT usename, usesysid FROM pg_user;
    

    您会注意到用户名是小写的。

    这是因为 PostgreSQL 将 SQL 语句中的所有字符折叠为小写(SQL 标准规定 SQL 不区分大小写)。

    但是,shell 区分大小写,因此您尝试使用不存在的用户名进行连接。

    一旦您获得了这种体验,您可能就会明白为什么在数据库对象名称中始终只使用小写字符和数字是个好主意。

    如果您坚持使用大写字符,则必须用双引号将名称括起来。

    【讨论】:

    • 这个信息很好知道,谢谢!不幸的是,我尝试创建一个全小写的新数据库,并以全小写登录,但不幸的是我遇到了同样的问题。
    • 好的,日志中有什么内容?查询产生了什么?
    【解决方案2】:

    虽然使用小写数据库和用户名重试所有操作都不起作用,但我能够通过另一种方法解决该问题。

    使用此问题的已接受答案 (How to thoroughly purge and reinstall postgresql on ubuntu?) 中概述的说明,我清除了计算机上与 PostgresSQL 相关的所有内容,然后重新安装。我无法登录: psql -d postgresdb -U postgresdbuserpsql -d postgresdb -U postgresdbuser -W

    但是,我能够使用以下命令登录数据库:psql -U postgresdbuser -h 127.0.0.1 -d postgresdb

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 2013-09-04
      • 2021-05-12
      相关资源
      最近更新 更多