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 OverflowYugabyteDB
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.
Videos
Node.js PostgreSQL Tutorial: Calling Functions & Database Operations ...
19:51
How to use PostgreSQL and connect it to NodeJS - YouTube
58:41
Using PostgreSQL with Node.js | codeLive - YouTube
05:37
How to Connect Node js to PostgreSQL Database and Fetch data - YouTube
12:33
Utilizando Node.js com PostgreSQL - YouTube
12:31
Connect Node.js with PostgreSQL using Prisma ORM | Test API with ...
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.
GitHub
github.com › brianc › node-postgres
GitHub - brianc/node-postgres: PostgreSQL client for node.js. · GitHub
Starred by 13.2K users
Forked by 1.3K users
Languages JavaScript 79.4% | TypeScript 19.6%
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…
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.
Google
docs.cloud.google.com › spanner › connect node-postgres to a postgresql-dialect database
Connect node-postgres to a PostgreSQL-dialect database | Spanner | Google Cloud Documentation
node-postgres is a Node.js driver for PostgreSQL.
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 › 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
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
Cloudflare
developers.cloudflare.com › directory › hyperdrive › examples › connect to postgresql › libraries and drivers › postgres.js
Postgres.js · Cloudflare Hyperdrive docs
April 21, 2026 - Postgres.js ↗ is a modern, fully-featured PostgreSQL driver for Node.js.
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
Top answer 1 of 13
10
I’m a big fan of postgres.js ( https://github.com/porsager/postgres ). It’s minial and straightforward but you have to know how to sql. In fact I don’t really like ORMs. Most of the time I feel it makes things over complicated for nothing and I’ll never use any other databases than postgres. Dev mode included. As everyone should in my opinion (trying to get each environements the same for testing/debuging purpose is a hughe plus). Moreover it’s fast.
2 of 13
8
Prisma & Drizzle are good for me. Drizzle is more lightweight and faster but Prisma is more mature and more abstracted. They are quite similar in many things. I had best experience with both. TypeORM - I don't really like it anymore. It has good TS support, I like declaring schema with decoratos but... There is .save() method and it performs UPSERTS. Sure, you have .update() and .insert() methods, but they return RAW results without entity typings. I also often find myself using TypeORM's query builder - there is also no typings for results. At the end of the day, I had to take care of mapping results to entity on my own. Sequelize - it was my first ORM, when started with Node.js; Nowadays it's well known for lack of good TypeScript support. MikroORM - I never used, but it seems to be similar to TypeORM. I hope it handles things better than TypeORM. Knex - it's fine query builder.