使用Drizzle ORM推送迁移时出现Postgres用户密码认证失败错误
Drizzle推送迁移时PostgreSQL密码认证失败解决指南
问题描述
使用Drizzle作为ORM可正常生成迁移文件,但执行迁移推送时触发密码认证失败错误:
error: password authentication failed for user "postgres" at D:\code\toni\inventory\server\node_modules\pg-pool\index.js:45:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at PgDialect.migrate (D:\code\toni\inventory\server\node_modules\src\pg-core\dialect.ts:61:3) at migrate (D:\code\toni\inventory\server\node_modules\src\node-postgres\migrator.ts:10:2) at main (d:\code\toni\inventory\server\src\db\migrate.ts:15:3) { length: 104, severity: 'FATAL', code: '28P01', detail: undefined, hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'auth.c', line: '326', routine: 'auth_failed' }
使用的连接字符串:postgresql://postgres:postgres@localhost:5432/testDB?schema=public
排查与解决步骤
验证postgres用户密码准确性
直接用psql客户端测试登录:终端输入psql -U postgres -d testDB,输入密码postgres。如果登录失败,说明密码错误,按以下步骤重置:- 找到PostgreSQL的
pg_hba.conf配置文件(Windows路径一般是C:\Program Files\PostgreSQL\[版本号]\data,Linux为/var/lib/postgresql/[版本号]/main) - 将文件中
local all postgres scram-sha-256这一行的认证方式改为trust - 重启PostgreSQL服务
- 终端输入
psql -U postgres,执行SQL语句:ALTER USER postgres WITH PASSWORD 'postgres'; - 改回
pg_hba.conf中的原认证方式,再次重启服务
- 找到PostgreSQL的
检查连接字符串配置细节
- 若密码包含
@、:、#等特殊字符,需要进行URL编码(比如#转成%23) - 确认端口
5432和PostgreSQL实际监听端口一致,可在postgresql.conf文件中查看port配置项 - 确认数据库
testDB已创建,通过psql登录后执行\l命令查看数据库列表
- 若密码包含
确认Drizzle配置的连接字符串是否正确
检查Drizzle的配置文件(如drizzle.config.ts)或自定义的迁移脚本,确保使用的连接字符串和你提供的完全一致,避免环境变量或硬编码覆盖导致的配置错误检查PostgreSQL服务运行状态
确保PostgreSQL服务处于运行状态:Windows可在系统服务列表中查看,Linux执行systemctl status postgresql命令检查
内容的提问来源于stack exchange,提问作者Jacob Mutale
相关产品推荐
相关产品推荐

