Postgresql/psycopg2 即使用户和密码正确,密码验证错误

Postgresql/psycopg2 password authentication error even though user and password are correct

我是 Python 的网络开发新手,非常感谢您的帮助。我正在尝试在 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" 

我很困惑为什么验证对有效的 user/password 组合失败!

我也尝试在 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

在 运行ning 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               20-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 设置
#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)

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