问题描述
我正在尝试使用Node和pg(https://node-postgres.com/)库连接到远程Postgresql实例(aurora)。
我的代码如下:
const { Pool } = require('pg');
const pool = new Pool({
user: '-not-shown-',host: '-not-shown-',database: '-not-shown-',password: '-not-shown-',port: 5432,});
console.log('Testing select Now query...');
pool.query('SELECT Now()',(err,res) => {
console.log(err,res);
pool.end();
if (!err) {
console.log(res);
} else {
console.log(err);
}
});
但是,我没有任何结果。无论是查询结果还是任何类型的错误。 请注意,我能够在命令行中使用psql成功连接到数据库实例。 我正在使用EC2免费套餐Amazon Linux 2 AMI实例。
该代码在我的本地计算机上工作,并且得到如下结果:
注意,没有结果,也没有错误。 我不知道发生了什么。 我认为有问题,因为如果可以的话,它会返回查询结果。 请帮忙。
解决方法
更新:
我找到了答案: https://github.com/brianc/node-postgres/issues/2069
当前版本的node-postgres和pg-promise与最新版本的node不兼容。我当时使用的是Node14。我将其降级为12,现在可以正常工作了。