Skip to content

认证失败

错误信息

                密码认证失败
              

快速摘要

数据库服务器 refused 你的登录因为用户名, 密码, or 认证方法做 不 match.

为什么发生

  • 错误密码 (typo or 更改密码)

  • 错误用户名

  • 用户做 不有 permission 到连接到 这个数据库

  • 认证方法 mismatch (e.g., md5 对比 scram-sha-256)

  • 用户做 不 exist in 数据库

最小示例

✗ 错误代码
# Wrong password
psql -U postgres -h localhost
# → password authentication failed for user "postgres"
✓ 修复代码
# Correct password
PGPASSWORD=correct_password psql -U postgres -h localhost

密码是 错误. Providing 正确密码 (通过 PGPASSWORD 环境 var) fixes 错误.

如何诊断

  • 验证用户名 and 密码是 正确

  • 检查如果用户 exists: \du in psql or SELECT * FROM pg_user

  • 验证用户有 permission 到连接到 数据库

  • 检查认证方法 in 服务器配置

如何修复

  • 正确用户名 and 密码 in 你的连接字符串

  • 创建用户如果它 做不 exist: CREATE USER name WITH PASSWORD '通过'

  • Grant 连接 permission: GRANT CONNECT ON DATABASE db TO 用户

  • 更新认证方法 in pg_hba.conf 如果需要

如何预防

  • 存储 credentials in 环境变量 or secrets manager

  • Use 连接 pooling 使用验证 credentials

  • Document 数据库 access 需求 for 新 team 成员

相关资源

相关术语

相关课程

← 返回语言