0

I'm newbie to the backend below it's my configuration for 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;

In production, I'm facing this below error but it works in local I tried connecting my prod DB in my local machine its working fine

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)

Below route are working fine in production but connection is not established to postgres

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

My EC2 Configuration

Inbound rule

enter image description here OutBound rule

enter image description here

My RDS Configuration

Inbound rule

enter image description here

OutBound rule

enter image description here

2 Answers 2

2

I suspect the RDS instance does not have a security group that allows connections from your EC2 instance. See this help article: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

If your RDS instance has a public IP address, then the domain name will resolve to that. If both your EC2 instance and RDS instance are hosted in the same VPC, then I recommend disabling the public IP address. This may make it harder to connect to the database from your personal computer, but you can use an SSH tunnel to accomplish in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer I have added an inbound and outbound rule for AWS could u please check that
It looks like the ports are open. Can you double check that the security group is attached to the RDS instance? And the EC2 instance? If you can SSH to the EC2 instance, the you can also try to connect using the psql command line utility. That may prove easier when testing the connectivity.
0

When setting up RDS for public access, it is not enough to open the ports in the Security Group.

You also need to select yes for "Publicly accessible", which is hidden inside the "Additional connectivity configuration" section (which is closed by default, so easy to overlook).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.