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 › apis › client
node-postgres
1 week ago - node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database.
🌐
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
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
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
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
Which Postgres client are you using?
Voted Slonik in— though my clients are mostly raw SQL with pg. Only recently started adopting Slonik since it was more “battle-tested” w/ patterns I had to eventually come up with/discover on my own. I’m of the opinion against “wasting” time learning specific ORMs. Same time commitment into SQL will yield WAY better results (imo) More on reddit.com
🌐 r/node
31
6
September 28, 2023
🌐
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

🌐
npm
npmjs.com › package › postgresql-client
postgresql-client - npm
July 22, 2024 - import {Connection} from 'postgresql-client'; // Create connection const connection = new Connection('postgres://localhost'); // Connect to database server await connection.connect(); // Execute query and fetch rows const result = await connection.query( 'select * from cities where name like $1', {params: ['%york%']}); const rows: any[] = result.rows; // Do what ever you want with rows // Disconnect from server await connection.close();
      » npm install postgresql-client
    
Published   Jul 22, 2024
Version   2.13.0
🌐
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 15159 other projects in the npm registry using pg.
      » npm install pg
    
Published   Jun 19, 2026
Version   8.22.0
🌐
GitHub
github.com › panates › postgrejs
GitHub - panates/postgrejs: Professional PostgreSQL client for NodeJS · GitHub
PostgreJS is an enterprise-level PostgreSQL client for Node.js. It is designed to provide a robust and efficient interface to PostgreSQL databases, ensuring high performance and reliability for enterprise applications.
Starred by 67 users
Forked by 14 users
Languages   TypeScript 99.4% | JavaScript 0.6%
Find elsewhere
🌐
Holt
sql.holt.courses › lessons › data › nodejs-and-postgresql
Node.js and PostgreSQL – Complete Intro to SQL
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.
🌐
npm
npmjs.com › package › postgres
postgres - npm
April 5, 2026 - Fastest full featured PostgreSQL client for Node.js. Latest version: 3.4.9, last published: 4 months ago. Start using postgres in your project by running `npm i postgres`. There are 1158 other projects in the npm registry using postgres.
      » npm install postgres
    
Published   Apr 05, 2026
Version   3.4.9
🌐
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, ...
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($1, $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($1, $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.

🌐
DEV Community
dev.to › thisdotmedia › connecting-to-postgresql-with-node-js-k6k
Connecting to PostgreSQL with Node.js - DEV Community
June 25, 2024 - $ docker run \ --name postgres \ -e POSTGRES_PASSWORD=yourpassword \ -p 5432:5432 \ -d postgres · I was able to verify everything was up and running by shelling in with: ... At this point you should have a fully functional database server running on your system! There are a couple of different ways to connect to your database. You can use a connection pool or just instantiate a client.
🌐
Northflank
northflank.com › guides › connecting-to-a-postgresql-database-using-node-js
Connecting to a PostgreSQL database using Node.js — Northflank
November 25, 2021 - Your local machine with Node & npm installed https://nodejs.org/ Create a new directory and initialize an empty node project with npm init · A running instance of Postgres with a database and user · Need to spin up a Postgres instance?Learn how to set up a free PostgreSQL database in minutes with Northflank · node-with-postgres/ ├─ connect.js <-- sets up postgres connection ├─ get-client.js <-- reuse client connections ├─ setup-table.js <-- example of creating a table in your DB ├─ add-data.js <-- example of writing to your tables ├─ read-data.js <-- example of reading from your tables ├─ package.json <-- created by `npm init`, set dependency versions ├─ index.js <-- http API server ├─ .env <-- optional - sets up your local environment variables └─ .gitignore <-- optional - avoid pushing node_modules and secrets to git
🌐
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 - const connectionString = ‘postgresql://dbuser:secretpassword@database.server.com:3211/mydb’ · const client = new Client({ connectionString: connectionString, client.connect · client.query(‘SELECT NOW()’, (err, res) => { console.log(err, res) client.end() }) Please refer to pg for more details · Nodejs ·
🌐
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 - node-postgres, or pg, is a nonblocking PostgreSQL client for Node.js. Essentially, node-postgres is a collection of Node.js modules for interfacing with a PostgreSQL database.
🌐
This Dot Labs
thisdot.co › blog › connecting-to-postgresql-with-node-js
Connecting to PostgreSQL with Node.js - This Dot Labs
February 14, 2023 - Here's how I set up the latest version of Postgres on my machine: ... At this point you should have a fully functional database server running on your system! There are a couple of different ways to connect to your database. You can use a connection pool or just instantiate a client.
🌐
Stack Abuse
stackabuse.com › using-postgresql-with-nodejs-and-node-postgres
Using PostgreSQL with Node.js and node-postgres
March 6, 2020 - Then, let's use npm to install the node-postgres module, which will be used to connect to and interact with Postgres: ... With our project bootstrapped, let's go ahead and configure the database. After that, we'll write some basic CRUD functionality. As with all relational databases, we'll start off by creating one and connecting to it. You can either use the CLI or a GUI-based client to do this.
🌐
ScaleGrid
help.scalegrid.io › docs › postgresql-connecting-to-nodejs-driver
Node.js PostgreSQL Connection
Run the following code to connect to you PostgreSQL deployment after you enter the information from step 2. ... const { Client } = require('pg') const client = new Client({ user: 'sgpostgres', host: 'SG-PostgreNoSSL-14-pgsql-master.devservers.scalegrid.io', database: 'postgres', password: 'password', port: 5432, }) client.connect(function(err) { if (err) throw err; console.log("Connected!"); });
🌐
npm
npmjs.com › package › node-postgres
node-postgres - npm
PostgreSQL client for node.js.
      » npm install node-postgres
    
Published   Jun 16, 2020
Version   0.6.2
🌐
Montecha
montecha.com › blog › node-js-and-postgresql-integration
Node.js & PostgreSQL Integration
April 28, 2021 - There are two ways in which you can connect to a PostgreSQL database. One of them is to use a single client, and the other is to use a connection pool.