【发布时间】: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