Node.js连接PostgreSQL报错“Unknown Authentication type”求助
Node.js连接PostgreSQL报"Unknown Authentication type"错误解决方法
我是Node.js和PostgreSQL新手,编写了如下连接代码:
import { Client } from "node-postgres"; (async () => { const client = new Client({ user: 'testuser', host: '127.0.0.1', database: 'test', password: 'password', port: 5432 }); await client.connect(); //const res = await client.query('SELECT * from pgtest'); //console.log(res); //await client.end(); })().catch(console.error);
运行后抛出如下错误:
throw new Error('Unknown Authentication type' + util.inspect({
这个错误大多和node-postgres与PostgreSQL的认证兼容性、依赖包或配置有关,直接按以下步骤排查:
检查PostgreSQL用户认证配置
找到PostgreSQL安装目录下的pg_hba.conf文件(通常在data子目录里),定位到对应127.0.0.1和testuser的配置行,确保认证方式是md5或scram-sha-256(这两种是pg包支持的类型)。如果是peer或ident,修改后重启PostgreSQL服务。修正依赖包导入问题
注意实际用于连接PostgreSQL的npm包是pg,而非node-postgres。先卸载错误的包,重新安装正确的依赖:npm uninstall node-postgres npm install pg然后把导入语句改成:
import { Client } from 'pg';核对连接参数准确性
确认user、database、password和PostgreSQL中创建的用户、数据库信息完全一致,避免拼写或大小写错误。
内容的提问来源于stack exchange,提问作者Hey
相关产品推荐
相关产品推荐

