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

问题描述

我是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",**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中的port和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的用户local的{​​{1}}设置为peer,所有用户的postgres都设置为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连接。