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

如何修复nodemon崩溃及PostgreSQL用户postgres密码认证失败问题?

解决PostgreSQL用户"postgres"密码认证失败问题

以下是针对error: password authentication failed for user "postgres"(错误码28P01)的具体解决步骤:

1. 核对应用数据库配置中的密码

  • 找到项目中的数据库配置文件(如db.js、config.js或.env环境变量文件)
  • 确认配置里的password字段与PostgreSQL中postgres用户的实际密码完全一致
  • 示例Node.js连接配置:
    const { Pool } = require('pg');
    const pool = new Pool({
      user: 'postgres',
      host: 'localhost',
      database: 'travel_tracker', // 你的数据库名
      password: 'your_correct_password', // 确保这里是正确密码
      port: 5432,
    });
    
  • 若使用.env文件,检查DB_PASSWORD等对应变量的值是否正确

2. 重置postgres用户密码(若忘记或不确定密码)

Windows系统操作:

  1. 打开命令提示符,进入PostgreSQL的bin目录(例如C:\Program Files\PostgreSQL\16\bin,版本号根据你的安装调整)
  2. 停止PostgreSQL服务:
    net stop postgresql-x64-16
    
  3. 以无认证模式启动服务:
    pg_ctl -D "C:\Program Files\PostgreSQL\16\data" start -o "-c listen_addresses='localhost'"
    
  4. 新开一个命令提示符,连接到PostgreSQL:
    psql -U postgres
    
  5. 执行重置密码命令(替换为你要设置的新密码):
    ALTER USER postgres WITH PASSWORD 'new_secure_password';
    
  6. 退出psql:\q
  7. 重启PostgreSQL服务:
    net start postgresql-x64-16
    

Linux/macOS系统操作:

  1. 停止PostgreSQL服务:
    sudo systemctl stop postgresql
    
  2. 编辑pg_hba.conf文件(路径通常为/var/lib/postgresql/16/main/pg_hba.conf),将所有md5/scram-sha-256替换为trust
  3. 重启服务:
    sudo systemctl start postgresql
    
  4. 连接到PostgreSQL:
    sudo -u postgres psql
    
  5. 重置密码:
    ALTER USER postgres WITH PASSWORD 'new_secure_password';
    
  6. 退出psql:\q
  7. 改回pg_hba.conf的认证方式为md5或scram-sha-256,再重启服务:
    sudo systemctl restart postgresql
    

3. 检查pg_hba.conf的认证规则

  • 打开pg_hba.conf文件,确保本地连接的认证方式正确,示例配置:
    # IPv4本地连接
    host    all             all             127.0.0.1/32            scram-sha-256
    # IPv6本地连接
    host    all             all             ::1/128                 scram-sha-256
    
  • 修改后必须重启PostgreSQL服务才能生效

4. 验证密码有效性

  • 使用psql命令直接测试连接,确认密码正确:
    psql -U postgres -d your_database_name -h localhost -p 5432
    
  • 若能成功进入psql交互环境,说明密码没问题,再回到应用配置检查是否完全匹配

内容的提问来源于stack exchange,提问作者ADIL NABI

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 01:10:59