【问题标题】:NodeJs and node-postgres code is not returning any response in EC2NodeJs 和 node-postgres 代码在 EC2 中没有返回任何响应
【发布时间】:2020-09-18 07:47:06
【问题描述】:

我正在尝试使用 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 实例。

代码在我的本地机器上运行,我得到的结果如下所示:

但是当我 ssh 进入 Ec2 机器时,我得到的只是:

注意,没有结果也没有错误。 我不知道发生了什么事。 我认为有问题,因为如果没问题,它会返回查询结果。 请帮忙。

【问题讨论】:

  • 您是否在代码中硬编码了机器公共 IP(作为数据库 IP)?如果是,请尝试使用“localhost”。以及检查您的节点服务器日志。

标签: node.js postgresql amazon-ec2 node-postgres


【解决方案1】:

更新:

我找到了答案: https://github.com/brianc/node-postgres/issues/2069

当前版本的 node-postgres 和 pg-promise 与最新版本的 node.js 不兼容。我使用的是 Node 14。我降级到了 12,现在它运行良好。

【讨论】:

  • 这不是真的。如果您真的按照您提供的链接进行操作,您会发现该问题仅存在于 8.0.3 之前的库版本中。前段时间已修复,最新版本与 NodeJS v14 没有任何问题。检查您的库版本。
猜你喜欢
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 2020-08-19
相关资源
最近更新 更多