使用Peewee连接PostgreSQL时出现认证失败问题求助
解决Peewee连接PostgreSQL的密码认证失败问题
问题根源
你的配置文件缺少PostgreSQL用户的密码参数,导致Peewee无法完成数据库认证,触发password authentication failed for user "postgres"错误。
解决步骤
1. 更新config.ini配置
在[DEFAULT]区块中添加psql_password字段,填入你的PostgreSQL用户密码:
[DEFAULT] discord_key = apikey # private token from discord developer porter psql_user = postgres # username for postgres database psql_password = your_actual_postgres_password # 替换为真实密码 psql_db = test # database name owner_id = owner # discord user ID of bot installation owner.
2. 修改bot.py的数据库连接逻辑
找到bot.py中初始化PostgresqlExtDatabase的代码段,添加password参数并读取配置中的密码值:
# 假设原代码使用configparser读取配置 import configparser config = configparser.ConfigParser() config.read('config.ini') # 初始化数据库连接时补充密码参数 db = PostgresqlExtDatabase( config['DEFAULT']['psql_db'], user=config['DEFAULT']['psql_user'], password=config['DEFAULT']['psql_password'], # 可选:若数据库不在本地,添加host/port参数,比如host='127.0.0.1', port=5432 )
额外注意事项
- 确保PostgreSQL的
postgres用户已设置对应密码,可通过PostgreSQL命令行执行ALTER USER postgres WITH PASSWORD 'your_password';修改。 - 检查PostgreSQL的
pg_hba.conf配置文件,确保对目标连接方式(如本地连接)启用密码认证(将认证方式设为md5或scram-sha-256)。
内容的提问来源于stack exchange,提问作者Ken Adams
相关产品推荐
相关产品推荐

