AWS EC2实例(生产)中的PostGres超时错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AWS EC2实例(生产)中的PostGres超时错误相关的知识,希望对你有一定的参考价值。

我是后端的新手,这是我对Postgres的配置

const pool = new Pool({
      user: "username",
      host: "hostname",
      database: "dbname",
      password: "postgres",
      port: 5432,
      max: 10,
      min: 10,
      statement_timeout: 10000,
      idleTimeoutMillis: 30000,
      connectionTimeoutMillis: 2000,
      maxUses: 7500,
    });

    console.log("requesting db");
    pool.connect((err, client, release) => {
      console.error("Error acquiring client", err);
      console.error("Error acquiring client", client);
      console.error("Error acquiring client", release);
      if (err) {
        return console.error("Error acquiring client", err.stack);
      }
    });
    pool.on("connect", () => {
      console.log("connected to the db");
    });
    pool.on("error", function (err, client) {
      console.log(client);
      console.log(err);
    });
    module.exports = pool;

在生产中,我遇到以下错误,但它在本地有效,我尝试将我的prod DB连接到本地计算机上,但工作正常

Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Error acquiring client undefined
Error acquiring client [Function: NOOP]
Error acquiring client Error: Connection terminated due to connection timeout
    at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
    at Object.onceWrapper (events.js:421:28)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
    at Socket.emit (events.js:315:20)
    at emitCloseNT (net.js:1656:8)
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
    at runNextTicks (internal/process/task_queues.js:66:3)
    at listOnTimeout (internal/timers.js:518:9)
    at processTimers (internal/timers.js:492:7)

以下路线在生产中运行正常,但未与Postgres建立连接

 app.get("/api/v1/test", function (req, res) {
      res.send("Hello World test!");
    });

我的EC2配置

入站规则

enter image description here出站规则

enter image description here

我的RDS配置

入站规则

enter image description here

出界规则

enter image description here

答案

我怀疑RDS实例没有允许来自您的EC2实例的连接的安全组。请参阅此帮助文章:https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

另一答案

设置RDS以供公共访问时,仅打开安全组中的端口是不够的。

以上是关于AWS EC2实例(生产)中的PostGres超时错误的主要内容,如果未能解决你的问题,请参考以下文章

调用某些外部 api url 时,aws EC2 中的 504 超时

AWS RDS / EC2:TimeoutError:Knex:获取连接超时。游泳池可能已满

使用 K6 - 3000 VU 轰炸 AWS EC2。连接开始超时,现在我无法通过 SSL 或 HTTP 请求连接到实例。这是啥?

如何从 aws rds 导出 postgres 数据库

将 EC2 实例连接到另一个 AWS 账户上的 RDS 卷

如何使用超时设置解决 AWS ELB/EC2 HTTP 503?