0% found this document useful (0 votes)
13 views3 pages

Welcome - Node-Postgres

node-postgres is a collection of Node.js modules designed for interfacing with PostgreSQL databases, offering features such as connection pooling, prepared statements, and async/await support. The documentation provides guidance on installation, version compatibility, and basic usage examples, including error handling and callback methods. Users are encouraged to explore advanced topics to fully leverage PostgreSQL's capabilities from Node.js.

Uploaded by

dooffice479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Welcome - Node-Postgres

node-postgres is a collection of Node.js modules designed for interfacing with PostgreSQL databases, offering features such as connection pooling, prepared statements, and async/await support. The documentation provides guidance on installation, version compatibility, and basic usage examples, including error handling and callback methods. Users are encouraged to explore advanced topics to fully leverage PostgreSQL's capabilities from Node.js.

Uploaded by

dooffice479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/18/25, 5:24 AM Welcome – node-postgres

node-postgres is a collection of node.js modules for interfacing with your PostgreSQL database. It
has support for callbacks, promises, async/await, connection pooling, prepared statements, cursors,
streaming results, C/C++ bindings, rich type parsing, and more! Just like PostgreSQL itself there are
a lot of features: this documentation aims to get you up and running quickly and in the right
direction. It also tries to provide guides for more advanced & edge-case topics allowing you to tap
into the full power of PostgreSQL from node.js.

Install

$ npm install pg

Supporters
node-postgres continued development and support is made possible by the many supporters.

If you or your company would like to sponsor node-postgres stop by GitHub Sponsors and sign up
or feel free to email me if you want to add your logo to the documentation or discuss higher tiers
of sponsorship!

Version compatibility
node-postgres strives to be compatible with all recent LTS versions of node & the most recent
"stable" version. At the time of this writing node-postgres is compatible with node 8.x, 10.x, 12.x
and 14.x To use node >= 14.x you will need to install [email protected] or later due to some internal
stream changes on the node 14 branch. Dropping support for an old node lts version will always be
considered a breaking change in node-postgres and will be done on major version number
changes only, and we will try to keep support for 8.x for as long as reasonably possible.

Getting started
The simplest possible way to connect, query, and disconnect is with async/await:

https://node-postgres.com 1/3
2/18/25, 5:24 AM Welcome – node-postgres

import pg from 'pg'


const { Client } = pg
const client = new Client()
await client.connect()

const res = await client.query('SELECT $1::text as message', ['Hello world!'])


console.log(res.rows[0].message) // Hello world!
await client.end()

Error Handling

For the sake of simplicity, these docs will assume that the methods are successful. In real life use,
make sure to properly handle errors thrown in the methods. A try/catch block is a great way to
do so:

import pg from 'pg'


const { Client } = pg
const client = new Client()
await client.connect()

try {
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
} catch (err) {
console.error(err);
} finally {
await client.end()
}

Callbacks

If you prefer a callback-style approach to asynchronous programming, all async methods support
an optional callback parameter as well:

import pg from 'pg'


const { Client } = pg
const client = new Client()

client.connect((err) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
console.log(err ? err.stack : res.rows[0].message) // Hello World!
client.end()

https://node-postgres.com 2/3
2/18/25, 5:24 AM Welcome – node-postgres

})
})

Our real-world apps are almost always more complicated than that, and I urge you to read on!

https://node-postgres.com 3/3

You might also like