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. Answer from Deleted User on reddit.com
🌐
node-postgres
node-postgres.com
node-postgres
node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database.
🌐
npm
npmjs.com › package › pg
pg - npm
June 19, 2026 - Non-blocking PostgreSQL client for Node.js.
      » npm install pg
    
Published   Jun 19, 2026
Version   8.22.0
Discussions

Which postgreSQL node.js client library to choose today?
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. More on reddit.com
🌐 r/node
47
27
August 17, 2023
postgresql - How to make connection to Postgres via Node.js - Stack Overflow
I find myself trying to create a postgres database, so I installed postgres and started a server with initdb /usr/local/pgsql/data, then I started that instance with postgres -D /usr/local/pgsql/da... More on stackoverflow.com
🌐 stackoverflow.com
PostgresJs: PostgreSQL client for Node.js and Deno
No “fancy” and bleeding abstractions like something like Prisma, not convoluted type annotations like other TS libs, no, just plain SQL statements in the best JavaScript way of doing it · I discovered learning to do Postgres a couple years ago, after getting sick of trying to hack Prisma, ... More on news.ycombinator.com
🌐 news.ycombinator.com
143
308
October 23, 2023
node.js - nodejs query postgres using WHERE - Stack Overflow
I have nodejs with a postgres db that i can run queries via API. If I can curl with localhost:3000/data/ID# and i get data back. The code below is working. const getDataById = (request, response) =... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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%
🌐
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

🌐
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
🌐
Holt
sql.holt.courses › lessons › data › nodejs-and-postgresql
Node.js and PostgreSQL – Complete Intro to SQL
No worries if you don't have a ton of experience with Node.js · We are going to be using the pg package which is the most common PostgreSQL client for Node.js. There are others but this is the one with the most direct access to the underlying queries which is what we want.
Find elsewhere
Top answer
1 of 8
326

Here is an example I used to connect node.js to my Postgres database.

The interface in node.js that I used can be found here https://github.com/brianc/node-postgres

var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword@localhost:5432/YourDatabase";

var client = new pg.Client(conString);
client.connect();

//queries are queued and executed one after another once the connection becomes available
var x = 1000;

while (x > 0) {
    client.query("INSERT INTO junk(name, a_number) values('Ted',12)");
    client.query("INSERT INTO junk(name, a_number) values(2)", ['John', x]);
    x = x - 1;
}

var query = client.query("SELECT * FROM junk");
//fired after last row is emitted

query.on('row', function(row) {
    console.log(row);
});

query.on('end', function() {
    client.end();
});



//queries can be executed either via text/parameter values passed as individual arguments
//or by passing an options object containing text, (optional) parameter values, and (optional) query name
client.query({
    name: 'insert beatle',
    text: "INSERT INTO beatles(name, height, birthday) values(2, $3)",
    values: ['George', 70, new Date(1946, 02, 14)]
});

//subsequent queries with the same name will be executed without re-parsing the query plan by postgres
client.query({
    name: 'insert beatle',
    values: ['Paul', 63, new Date(1945, 04, 03)]
});
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['john']);

//can stream row results back 1 at a time
query.on('row', function(row) {
    console.log(row);
    console.log("Beatle name: %s", row.name); //Beatle name: John
    console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
    console.log("Beatle height: %d' %d\"", Math.floor(row.height / 12), row.height % 12); //integers are returned as javascript ints
});

//fired after last row is emitted
query.on('end', function() {
    client.end();
});

UPDATE:- THE query.on function is now deprecated and hence the above code will not work as intended. As a solution for this look at:- query.on is not a function

2 of 8
37

A modern and simple approach: pg-promise:

const pgp = require('pg-promise')(/* initialization options */);

const cn = {
    host: 'localhost', // server name or IP address;
    port: 5432,
    database: 'myDatabase',
    user: 'myUser',
    password: 'myPassword'
};

// alternative:
// var cn = 'postgres://username:password@host:port/database';

const db = pgp(cn); // database instance;

// select and return a single user name from id:
db.one('SELECT name FROM users WHERE id = $1', [123])
    .then(user => {
        console.log(user.name); // print user name;
    })
    .catch(error => {
        console.log(error); // print the error;
    });

// alternative - new ES7 syntax with 'await':
// await db.one('SELECT name FROM users WHERE id = $1', [123]);

See also: How to correctly declare your database module.

🌐
Hacker News
news.ycombinator.com › item
PostgresJs: PostgreSQL client for Node.js and Deno | Hacker News
October 23, 2023 - No “fancy” and bleeding abstractions like something like Prisma, not convoluted type annotations like other TS libs, no, just plain SQL statements in the best JavaScript way of doing it · I discovered learning to do Postgres a couple years ago, after getting sick of trying to hack Prisma, ...
🌐
Medium
medium.com › @mateogalic112 › how-to-build-a-node-js-api-with-postgresql-and-typescript-best-practices-and-tips-84fee3d1c46c
How to Build a Node.js API with PostgreSQL and TypeScript: Best Practices and Tips | by Mateo Galic | Medium
November 17, 2024 - docker run -d \ --name node-postgres-demo \ -e POSTGRES_PASSWORD=postgres \ -e POSTGRES_USER=postgres \ -e POSTGRES_DB=node-postgres-demo \ -p 5432:5432 \ postgres
🌐
YouTube
youtube.com › kindson the tech pro
How to Connect Node js to PostgreSQL Database and Fetch data - YouTube
This video explains how to connect Node.js to PostgreSQL database and fetch data step by stepNode.js Tutorials Steps - https://www.kindsonthegenius.com/nodej...
Published   April 24, 2021
Views   220K
🌐
Viblo
viblo.asia › p › maybe-you-dont-know-using-node-postgres-in-nodejs-express-7ymJXxwPJkq
🤔MAYBE YOU DON'T KNOW - 👌Using Node-Postgres in Node.js Express✨
March 22, 2023 - Node-Postgres is a popular PostgreSQL client library for Node.js. It allows you to interact with a PostgreSQL database easily and efficiently.
🌐
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 - The pg-native driver leverages libpq, the official PostgreSQL C client library. This allows faster query parsing, lower overhead on network I/O, and better memory handling than the pure JavaScript implementation. Essentially, pg-native skips part of the JS-to-database abstraction cost. Despite being pure JavaScript, pg (AKA node-postgres) remains very efficient and stable.
🌐
Stack Overflow
stackoverflow.com › questions › 76168284 › nodejs-query-postgres-using-where
node.js - nodejs query postgres using WHERE - Stack Overflow
I have nodejs with a postgres db that i can run queries via API. If I can curl with localhost:3000/data/ID# and i get data back. The code below is working. const getDataById = (request, response) =...
🌐
Fek
fek.io › blog › using-postgres-and-timescale-db-with-node-js-series
Using Postgres and TimescaleDB with Node.js series
June 9, 2021 - I am going to be giving a presentation ... not familiar with Postgres, it is a relation database server that is very popular in the open source world, but also used heavily by large organizations....
🌐
OVHcloud
us.ovhcloud.com › home › how to access a postgresql from node.js application ?
How to access a PostgreSQL from Node.js application ?
To know how to install PostgreSQL see the tutorial How to install PostgreSQL on Ubuntu 22.04. You could use an IDE to facilitate the source file manipulation of this tutorial. You can have look at VS Code, WebStorm, … · In this tutorial, you will, first, install the Node.js node-postgres library, then, you will use it in a Node.js application.
🌐
LogRocket
blog.logrocket.com › home › crud rest api with node.js, express, and postgresql
CRUD REST API with Node.js, Express, and PostgreSQL - LogRocket Blog
March 27, 2026 - We’ll also walk through connecting an Express server with PostgreSQL using node-postgres. Our API will be able to handle the HTTP request methods that correspond to the PostgreSQL database from which the API gets its data.
🌐
Medium
medium.com › swlh › connect-postgresql-with-node-js-4ded3e81e31a
Connect PostgreSQL with Node.js. Here we are using the most popular… | by Amol Gunjal | The Startup | Medium
July 9, 2020 - Here we are using the most popular Node.js module ‘node-postgres’ for connecting the PostgreSQL database with Node.js.
🌐
Hevo
hevodata.com › home › learn › data integration
How to Connect Node Postgres using NPM? - Learn
March 30, 2026 - Open-source flexibility for complex Postgres flows · Limited documentation · State persistence challenges with node failover · Running long SQL queries can be difficult · Setup for complex Postgres pipelines requires expertise · Free, as it is open-source.
🌐
GitHub
github.com › BrandonZacharie › node-postgres
GitHub - BrandonZacharie/node-postgres: A PostgreSQL server manager for Node.js · GitHub
A PostgreSQL server manager for Node.js. Contribute to BrandonZacharie/node-postgres development by creating an account on GitHub.
Author   BrandonZacharie