Skip to content

pool.connect() waits forever #2338

@joker-8989

Description

@joker-8989

When listing the node versions, I noticed that I was forever waiting for pool.connect() to process.

import {Client, Pool} from "pg";
import * as fs from "fs";
import * as process from "process";

export interface DatabaseInterface {
    connect(): Promise<void>;

    end(): Promise<void>;

    getClient(): Promise<Client>;
}

export class Db implements DatabaseInterface {
    private client: Client;
    private pool: Pool;

    constructor() {
        const env = process.env["ENV"] || process.env.NODE_ENV || "dev";
        console.log(`DETECT ENV:: ${env}`);

        const config = JSON.parse(
            fs.readFileSync("./config/database.json", "utf-8")
        )[env];

        console.log(config)

        this.pool = new Pool(config);
    }

    async getClient(): Promise<Client> {
        return this.client;
    }

    async connect(): Promise<void> {
        this.client = await this.pool.connect();
    }

    async end(): Promise<void> {
        this.client.release();
        await this.pool.end();
    }

}

My environment is as follows.
Just in case, I tried to match the pg version to 7.3.3, but the result did not change.

# Before update
Node: v8.12.0
pg: 7.3.0
# After update
Node: v12.18.3
pg: 8.3.3

The contents of console.log (config) are as follows
I'm using Postgres 11.8 on AWS but I don't think it's a network issue as I can do the DB connection itself.

{
    "driver": "pg",
    "host": "xxxxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com",
    "database": "testdb",
    "user": "test",
    "password": "test1234",
    "port": 5432
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions