Export a factory function that takes the configuration object and returns an instance of the client. Client isn't a default export in pg so it needs to be destructured.

const { Client } = require('pg');

const createPgClient = (conf) => {
    const pgClient = new Client(conf)({
        host: conf['postgres'].host,
        port: conf['postgres'].port,
        dbname: conf['postgres'].dbname,
        username: conf['postgres'].username,
        password: conf['postgres'].password,
        dbconn: null,
    });

    pgClient.prototype.connect = function (callback) {

        client.connect()
        .then(() => {
            console.log(new Date() + " | Postgres Server Authenticated...");
        })
        .catch((err) => {
            console.log(new Date() + " | Postgres Server Error in connection...");
            console.log(err);
        })
        .finally(()=> {
            self.dbconn = this;
            callback(this);
        });

    };
    return pgClient;
};
module.exports = createPgClient;

Use this factory function in your app.js to create a client.

const createPgClient = require('./adapters/postgres')
cont pgClient = createPgClient(conf);

pg.connect(function (dbconn) {
    app.dbconn = dbconn;
    app.conf = conf;

    console.log("************************************************************");
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']);
    console.log("************************************************************");

    server.listen(conf['web']['port']);

    const Routes = require('./routes/http-routes');
    new Routes(app);
});
Answer from Oluwafemi Sule on Stack Overflow
🌐
DEV Community
dev.to › nigrosimone › benchmarking-postgresql-drivers-in-nodejs-node-postgres-vs-postgresjs-17kl
Benchmarking PostgreSQL Drivers in Node.js: node-postgres vs postgres.js, who is the faster? - DEV Community
September 7, 2025 - 1.10 mb) 13.97 kb ▁████... (pg) 1.25x faster than porsager/postgres (postgres) The pg-native driver leverages libpq, the official PostgreSQL C client library....
🌐
YugabyteDB
docs.yugabyte.com › stable › develop › drivers-orms › nodejs › postgres-pg-reference
PostgreSQL node-postgres Driver | YugabyteDB Docs
November 25, 2025 - The PostgreSQL node-postgres driver is the official Node.js driver for PostgreSQL. The YSQL API is fully compatible with the PostgreSQL node-postgres (pg) driver, so you can connect to the YugabyteDB database to execute DMLs and DDLs using the node-postgres APIs.
🌐
PostgreSQL
postgresql.org › message-id › 157851397836.29176.4731594661906491622@wrigleys.postgresql.org
PostgreSQL: Postgres.js driver - for Node.js
January 8, 2020 - I recently finished a fast, 0 dependency driver for Node.js with a very user friendly api. I think would be great to include in the documentation and the Wiki page (details below). You can see it here https://github.com/porsager/postgres.
🌐
YugabyteDB
docs.yugabyte.com › preview › drivers-orms › nodejs › postgres-node-driver
node-postgres Driver for YSQL | YugabyteDB Docs
November 15, 2025 - The PostgreSQL node-postgres driver is the official Node.js driver for PostgreSQL which can be used to connect with YugabyteDB YSQL API.
🌐
w3tutorials
w3tutorials.net › blog › nodejs-postgres-driver
Node.js PostgreSQL Driver: A Comprehensive Guide — w3tutorials.net
A Node.js PostgreSQL driver acts as a bridge between the Node.js application and the PostgreSQL database. It allows developers to execute SQL queries, manage transactions, and interact with the database in a seamless manner.
Find elsewhere
🌐
GitHub
github.com › brianc › node-postgres
GitHub - brianc/node-postgres: PostgreSQL client for node.js. · GitHub
Non-blocking PostgreSQL client for Node.js (and bun, deno, cloudflare, etc...).
Starred by 13.2K users
Forked by 1.3K users
Languages   JavaScript 79.4% | TypeScript 19.6%
🌐
CData
cdata.com › kb › tech › postgresql-odbc-nodejs.rst
Query PostgreSQL Data through ODBC in Node.js
Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a browser. With the CData ODBC Driver for PostgreSQL, you can access live PostgreSQL data from Node.js apps and scripts.
🌐
node-postgres
node-postgres.com
node-postgres
node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database.
🌐
ScaleGrid
help.scalegrid.io › docs › postgresql-connecting-to-nodejs-driver
Node.js PostgreSQL Connection
{` `} Easily connect your PostgreSQL hosting deployments at ScaleGrid to the Node.js driver to optimize your PostgreSQL management in the cloud. How to Connect PostgreSQL to the Node.js Driver Install the PostgreSQL extension for Node.js npm install pg Get the following PostgreSQL deployment informa…
🌐
Devart
devart.com › odbc › postgresql › integrations › postgresql-nodejs-connection.html
PostgreSQL Node.js ODBC Connection Driver - Download Connector for Integration and Sync
Integrate PostgreSQL and Node.js using ODBC Driver 32/64-bit by Devart. Sync PostgreSQL data to Node.js with direct connection. Free 30 days trial!
🌐
YugabyteDB
docs.yugabyte.com › stable › develop › drivers-orms › nodejs › postgres-node-driver
node-postgres Driver for YSQL - Yugabyte Docs - YugabyteDB
November 15, 2025 - The PostgreSQL node-postgres driver is the official Node.js driver for PostgreSQL which can be used to connect with YugabyteDB YSQL API.
🌐
GitHub
github.com › porsager › postgres
GitHub - porsager/postgres: Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare · GitHub
Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare - porsager/postgres
Starred by 8.7K users
Forked by 355 users
Languages   JavaScript
🌐
npm
npmjs.com › package › pg
pg - npm
June 19, 2026 - PostgreSQL client - pure javascript & libpq with the same API. Latest version: 8.22.0, last published: a month ago. Start using pg in your project by running `npm i pg`. There are 15136 other projects in the npm registry using pg.
      » npm install pg
    
Published   Jun 19, 2026
Version   8.22.0
🌐
GitHub
github.com › li-plaintext › nodejs_psql
GitHub - li-plaintext/nodejs_psql: postgres driver · GitHub
Note: node_psql is for exploring postgres database purpose · More Information: https://www.npmjs.com/package/nodejs_psql
Author   li-plaintext
🌐
Reddit
reddit.com › r/node › which postgresql node.js client library to choose today?
r/node on Reddit: Which postgreSQL node.js client library to choose today?
August 17, 2023 -

Raw queries, ORM, Query builder, code generators etc which pg client library would you choose with Node.js today in production?

Popular ones are: 1] Knex 2] Sequalize 3] TypeORM 4] Prisma 5] Drizzle 6] MikroORM

If you can also comment on "why" that would also be great. If there is any new recommendation that is also great