You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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。如果登录失败,说明密码错误,按以下步骤重置:

    1. 找到PostgreSQL的pg_hba.conf配置文件(Windows路径一般是C:\Program Files\PostgreSQL\[版本号]\data,Linux为/var/lib/postgresql/[版本号]/main)
    2. 将文件中local all postgres scram-sha-256这一行的认证方式改为trust
    3. 重启PostgreSQL服务
    4. 终端输入psql -U postgres,执行SQL语句:ALTER USER postgres WITH PASSWORD 'postgres';
    5. 改回pg_hba.conf中的原认证方式,再次重启服务
  • 检查连接字符串配置细节

    • 若密码包含@、:、#等特殊字符,需要进行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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:58:24