node-postgres is a free, open source databases project written in JavaScript and released under MIT. It has 13,209 GitHub stars, 1,353 forks and 528 open issues, and was last pushed 7 days ago. On this registry it ranks #60 of 143 tracked projects in Databases, with 5 head-to-head comparisons available.

What is node-postgres?

node-postgres is an MIT-licensed, non-blocking PostgreSQL client for Node.js — and for bun, deno, cloudflare and comparable runtimes — built for JavaScript developers who need application code to speak to a PostgreSQL database.

What it is

node-postgres is a monorepo published under the node-postgres name that provides the core pg module together with a set of supporting modules: pg-pool, pg-native, pg-cursor, pg-query-stream, pg-connection-string and pg-protocol. It lives in the Node.js ecosystem and targets PostgreSQL as its one and only database. The client is written in pure JavaScript, and the repository also ships optional native libpq bindings; both paths expose the same API, so an application can move between them without rewriting query code. The wire protocol itself is implemented inside the project through the pg-protocol package.

The concrete thing it replaces is the need to reach PostgreSQL through native libpq bindings, a C toolchain, or a hand-rolled implementation of the PostgreSQL frontend/backend protocol. Teams that install pg get a JavaScript client that connects, authenticates, sends parameterized queries and reads results without a compiled dependency, while pg-native remains available for anyone who does want libpq underneath. Around that core it solves the routine problems of talking to PostgreSQL from an application: keeping a pool of live connections instead of opening one per request, coercing values between JavaScript and PostgreSQL types, reading very large result sets incrementally, and moving bulk data in and out.

Key capabilities

  • Connection pooling through the pg-pool module, so a server process can reuse a bounded set of PostgreSQL connections.
  • Pure JavaScript client and native pg-native libpq bindings that share the same API, allowing an application to switch between them.
  • Extensible JavaScript to PostgreSQL data-type coercion, so custom types can be mapped in both directions.
  • Parameterized queries that pass values separately from SQL text.
  • Named statements with query plan caching.
  • Asynchronous notifications using PostgreSQL LISTEN/NOTIFY.
  • Bulk import and export through COPY TO and COPY FROM.
  • Cursor and streaming access to large results via pg-cursor and pg-query-stream.

Who uses it and how

  • Node.js application servers that need a pooled connection to one or more PostgreSQL databases and issue parameterized queries per request.
  • Runtimes beyond Node.js, including bun, deno and cloudflare, which the README calls out as supported targets for the pure JavaScript client.
  • Services that react to database events, using LISTEN/NOTIFY to receive asynchronous notifications rather than polling.
  • Data pipelines and reporting jobs that read large result sets in batches through pg-cursor or pg-query-stream instead of loading everything into memory.
  • Teams that need maximum throughput and are willing to build the native bindings, which are only built when libpq-dev is present.

Getting started

Install the core package with npm install pg. Documentation for the project and all related modules lives at https://node-postgres.com, and contributors developing the repo run yarn followed by yarn lerna bootstrap from the workspace root.

How it compares

The facts provide no list of paid products that node-postgres replaces, and no competing client is named. It stands alone in this registry: the other packages mentioned alongside it — pg-pool, pg-native, pg-cursor, pg-query-stream, pg-connection-string and pg-protocol — are modules inside the same monorepo, not alternatives to it.

When to use it — and when not to

Choose it when the goal is a thin, well-documented PostgreSQL driver for JavaScript runtimes and the application is willing to own its own query layer; the README states that node-postgres is by design light on abstractions and points to a wiki list of extra modules for anything more. Be aware that adopting it means running a PostgreSQL server yourself, and that local development against this repository requires libpq-dev plus a PostgreSQL instance with SSL enabled and an empty database for the test suite. It is a poor fit for teams that want an ORM or schema tooling out of the box, and the 528 open issues, with overall documentation hosted outside the repository on a separate site, are worth weighing before committing to it as a critical dependency.

project readme (upstream, from github) — read inline

node-postgres

Build Status NPM version NPM downloads

Non-blocking PostgreSQL client for Node.js (and bun, deno, cloudflare, etc...). Pure JavaScript and optional native libpq bindings.

Monorepo

This repo is a monorepo which contains the core pg module as well as a handful of related modules.

Install

npm install pg

Documentation

Each package in this repo should have its own readme more focused on how to develop/contribute. For overall documentation on the project and the related modules managed by this repo please see:

:star: Documentation :star:

The source repo for the documentation is available for contribution here.

Features

  • Fastest PostgreSQL client for Node.js
  • Pure JavaScript client and native libpq bindings share the same API
  • Connection pooling
  • Extensible JS ↔ PostgreSQL data-type coercion
  • Supported PostgreSQL features
    • Parameterized queries
    • Named statements with query plan caching
    • Async notifications with LISTEN/NOTIFY
    • Bulk import & export with COPY TO/COPY FROM

Extras

node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture. The entire list can be found on our wiki.

Support

node-postgres is free software. If you encounter a bug with the library please open an issue on the GitHub repo. If you have questions unanswered by the documentation please open an issue pointing out how the documentation was unclear & I will do my best to make it better!

When you open an issue please provide:

  • version of Node
  • version of Postgres
  • smallest possible snippet of code to reproduce the problem

You can also follow me @brianc on bluesky if that's your thing for updates on node-postgres with nearly zero non node-postgres content. My old twitter/x account is no longer used.

Sponsorship :two_hearts:

node-postgres's continued development has been made possible in part by generous financial support from the community.

If you or your company are benefiting from node-postgres and would like to help keep the project financially sustainable please consider supporting its development.

Featured sponsor

Special thanks to medplum for their generous and thoughtful support of node-postgres!

Medplum logo

Contributing

:heart: contributions!

I will happily accept your pull request if it:

  • has tests
  • looks reasonable
  • does not break backwards compatibility

If your change involves breaking backwards compatibility please please point that out in the pull request & we can discuss & plan when and how to release it and what type of documentation or communication it will require.

Setting up for local development

  1. Clone the repo
  2. Ensure you have installed libpq-dev in your system (the native bindings are built in the test process)
  3. From your workspace root run yarn and then yarn lerna bootstrap
  4. Ensure you have a PostgreSQL instance running with SSL enabled and an empty database for tests. note: you can skip the tests requring SSL by setting the environment variable PGTESTNOSSL=1 if you're not changing any SSL related code.
  5. Ensure you have the proper environment variables configured for connecting to your postgres instance. Using the standard PG* environment variables like PGUSER and PGPASSWORD etc...
  6. Run yarn test to run all the tests.

Troubleshooting and FAQ

The causes and solutions to common errors can be found among the Frequently Asked Questions (FAQ)

License

Copyright (c) 2010-2020 Brian Carlson ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Frequently asked questions

Is node-postgres free to use?

node-postgres is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does node-postgres do?

PostgreSQL client for node.js.

What is node-postgres written in?

node-postgres is primarily written in JavaScript. Its source is publicly available at https://github.com/brianc/node-postgres, and it has 13,209 GitHub stars.