LoginSignup
0
0

More than 1 year has passed since last update.

[Node.js][PostgreSQL] too many connections for role の解決

Last updated at Posted at 2022-01-16

設定

  • PostgreSQL 13.5
  • Node.js 14.16.1
  • pg(node-postgres) 8.7.1

問題

  • Node.jsからDBに対するクエリのレスポンスが返ってこない
    • 以下のコードのresultsが返らない
model.js
exports.getUserByUserId = async function (user_id) {
  var query = {
    text: "select * from users where user_id = $1;",
    values: [user_id],
  };
  await pool.connect();
  try {
    results = await pool.query(query);
    return results.rows[0];
  } catch (e) {
    console.log(e);
  }
};
  • psqlでCUIから接続しようとすると以下のエラーで接続できない
psql: error: connection to server at [name] ([ip_address]), port [port_number] 
failed: FATAL:  too many connections for role [name]
 ▸    psql exited with code 2

先行事例の調査

原因

  • connectionが溜まっている

解決

  • connectionをreleaseする
model.js
exports.getGroupByGroupId = async function (group_id) {
  var query = {
    text: "select * from groups where group_id = $1;",
    values: [group_id],
  };
  client = await pool.connect();
  try {
    results = await client.query(query);
    client.release();
    return results.rows[0];
  } catch (e) {
    console.log(e);
  }
};

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0