我正在使用pg
模块在Node.js中编写一个简单的“连接数据库”程序。
直到昨天,一切正常,但现在由于某种原因出现此错误:
错误:用户“ ezert”的密码身份验证失败
我不知道发生了什么,因为我知道脚本中使用的密码是正确的。
完整代码:
const { Client } = require('pg'); // quando definido entre parêntesis, declara-se que se trata de um contrutor
let connectionURL = 'postgresql://ezert:<MY PASSWORD>@localhost:5432/mydb';
const client = new Client({
connectionURL: connectionURL
});
client.connect();
client.query("SELECT * FROM my_table", function (err, result) {
if (err) {
console.log('[ERROR]:\n' + err);
return;
}
console.log('[RESULT]:');
console.log(result.rows);
return;
});
这是PostgreSQL的问题,不是Node.js的问题。
解决:
/etc/postgresql/YOUR_PG_VERSION/main/pg_hba.conf
postgres
用户的Unix密码。psql -h localhost -U postgres
登录,使用在(3)中选择的密码。pg_hba.conf
文件重置为默认值。