【问题标题】:Postgresql/psycopg2 password authentication error even though user and password are correct即使用户和密码正确,Postgresql/psycopg2 密码验证错误
【发布时间】:2020-08-12 02:06:15
【问题描述】:

我是使用 Python 进行 Web 开发的新手,非常感谢一些帮助。我正在尝试在 WSL(Windows 机器)上设置 psycopg2/peewee。在我的 Python 代码中,我存储了访问 Postgres 数据库所需的所有信息,然后尝试按如下方式连接到数据库:

#lib/main.py
from peewee import *
db = PostgresqlDatabase('people', user='postgres', password='postgres', host='localhost', port=5432)
db.connect()

我确定我有一个名为“postgres”的 Postgres 用户,密码为“postgres”。这些是我使用 su - postgres 在我的机器上更改为 postgres 用户后用来启动 Postgres 服务器的凭据。

当我使用 pipenv shell 启动我的 Python 虚拟环境,然后运行 ​​python3 main.py 时,我收到以下错误:

// ♥  python3 main.py
Traceback (most recent call last):
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 3035, in connect
    self._state.set_connection(self._connect())
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 3730, in _connect
    conn = psycopg2.connect(database=self.database, **self.connect_params)
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
**psycopg2.OperationalError: FATAL:  password authentication failed for user "postgres"**  

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    db.connect()
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 3038, in connect
    self._initialize_connection(self._state.conn)
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 2873, in __exit__
    reraise(new_type, new_type(exc_value, *exc_args), traceback)
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 183, in reraise
    raise value.with_traceback(tb)
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 3035, in connect
    self._state.set_connection(self._connect())
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py", line 3730, in _connect
    conn = psycopg2.connect(database=self.database, **self.connect_params)
  File "/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
peewee.OperationalError: FATAL:  password authentication failed for user "postgres" 

我很困惑为什么有效用户/密码组合的身份验证失败!

我也尝试在 Python3 shell 中测试连接字符串:

>>>import psycopg2
>>>psycopg2.connect("dbname=postgres user=postgres host=localhost password=postgres port=5432")

我得到同样的错误。我非常感谢任何关于为什么我无法将我的 Python 代码连接到 Postgres 的见解!

我的 pg_hba.conf 文件有以下内容:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# 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                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

从命令行,ps ax |grep postgres 显示:

 3218 ?        S      0:00 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf
 3220 ?        Ss     0:00 postgres: 10/main: checkpointer process
 3221 ?        Ss     0:00 postgres: 10/main: writer process
 3222 ?        Ss     0:00 postgres: 10/main: wal writer process
 3223 ?        Ss     0:00 postgres: 10/main: autovacuum launcher process
 3224 ?        Ss     0:00 postgres: 10/main: stats collector process
 3225 ?        Ss     0:00 postgres: 10/main: bgworker: logical replication launcher
 3337 tty1     S      0:00 grep --color=auto postgres

在运行psql -d postgres -U postgres -h 127.0.0.1 并检查 Postgres 日志后,我看到了:

2020-08-12 13:37:30.573 EDT [709] LOG:  database system is shut down
2020-08-12 13:37:30.700 EDT [3218] LOG:  listening on IPv4 address "127.0.0.1", port 5433                                                              2020-08-12 13:37:30.714 EDT [3218] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2020-08-12 13:37:30.760 EDT [3219] LOG:  database system was shut down at 2020-08-12 13:37:30 EDT
2020-08-12 13:37:30.816 EDT [3218] LOG:  database system is ready to accept connections
2020-08-12 13:37:31.383 EDT [3226] [unknown]@[unknown] LOG:  incomplete startup packet
2020-08-12 13:37:48.061 EDT [3254] postgres@postgres LOG:  provided user name (postgres) and authenticated user name (allison_johnson) do not match
2020-08-12 13:37:48.061 EDT [3254] postgres@postgres FATAL:  Peer authentication failed for user "postgres"
2020-08-12 13:37:48.061 EDT [3254] postgres@postgres DETAIL:  Connection matched pg_hba.conf line 85: "local   all             postgres               $2020-08-12 13:42:30.909 EDT [3220] WARNING:  could not flush dirty data: Function not implemented                                                      2020-08-12 14:08:00.001 EDT [3443] allison_johnson@allison_johnson FATAL:  password authentication failed for user "allison_johnson"
2020-08-12 14:08:00.001 EDT [3443] allison_johnson@allison_johnson DETAIL:  Password does not match for user "allison_johnson".
        Connection matched pg_hba.conf line 90: "local   all             all                                     md5"
2020-08-12 14:13:49.143 EDT [3514] postgres@postgres LOG:  provided user name (postgres) and authenticated user name (allison_johnson) do not match
2020-08-12 14:13:49.143 EDT [3514] postgres@postgres FATAL:  Peer authentication failed for user "postgres"
2020-08-12 14:13:49.143 EDT [3514] postgres@postgres DETAIL:  Connection matched pg_hba.conf line 85: "local   all             postgres               $

pg_hba.conf 的第 85-90 行:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5

/main/postgresql.conf 中的端口和监听地址设置

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5433                             # (change requires restart)

【问题讨论】:

  • 看起来我们将聊天带入了新的一天(UTC),事情变得异常了。我要问的是:sudo nano /etc/postgresql/10/main/postgresql.conf 并报告端口和listen_addresses的设置。
  • 我会将我在上面文件中看到的内容粘贴到问题中......看起来可能没有listen_addresses?
  • 您在代码中使用localhost,但在运行psql 命令行时使用127.0.0.1。这些可能不等价(至少有一个数据库驱动程序假定localhost 表示use the unix socket,我不记得那是postgres 还是其他东西)。
  • @larsks。我建议通过整个聊天讨论。这比你想象的要复杂。

标签: python postgresql psycopg2 peewee


【解决方案1】:

答案的简短版本是,两个 Postgres 实例在两个不同的操作系统中运行。较长的答案如下。在 WSL(2) 中,Postgres 实例pg_hba.conflocal 设置为peer 用户postgresmd5 所有人。那里有 md5 本地主机 IPV4 和 IPV6 的身份验证连接。这就是为什么 Allison 可以从系统用户 postgres 帐户以数据库 userpostgres 用户身份连接的原因。更改local trust 的设置允许在不使用 -h 时从任何系统帐户进行​​连接。 localhost 的密码连接问题仍然存在。奇怪的是 Postgres 日志中没有这些连接的记录。没有其他 Postgres 实例在 WSL 中运行。在我考虑了太久之后,很明显必须有一台服务器在 Windows 中运行。情况就是这样,它是抓住localhost 连接并抛出密码错误的那个。关闭它是解决这个问题的一个步骤。结果,Windows Postgres 正在端口5432 和WSL 实例5433 上运行,而WSL 实例仅在localhost 上侦听。将端口更改为 5432 并将 listen_addresses 更改为 '*' 允许 localhost 连接而不指定端口和 local 连接。

【讨论】:

  • 哦,我现在明白了...但我的理解是有两个 Postgres 实例,一个在端口 5432 上运行,另一个在端口 5433 上运行。OP 的密码正确5433,但代码调用的是 5432 中的那个。似乎只需要更改端口。
  • 除了 OP 只是想与 WSL 中的一个一起工作,因此使用默认端口运行并关闭另一个以降低混乱程度更有意义。如果他们决定运行 Windows 实例,他们可以在那时更改端口号。
猜你喜欢
  • 2020-05-31
  • 2012-05-23
  • 2013-09-18
  • 2015-02-19
  • 2017-07-26
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多