Library / SDK
vitaly-t/pg-promise avatar
vitaly-t/pg-promise

pg-promise is named after a feature its own README calls a tiny part

PostgreSQL interface for Node.js

3,548 stars219 forksJavaScriptMIT

At a glance

What is it?
pg-promise is a PostgreSQL interface for Node.js built on top of the pg driver, whose main contribution is a query formatter that escapes values. The formatter and JavaScript template literals claim the same syntax, one peer dependency is pinned to a single patch version, and every dependency in the manifest is pinned exactly.
Who is it for?
pg-promise earns its place when you want the connection handling and the transaction scoping that the base driver leaves to you, and when you would rather hand SQL to a formatter than assemble strings yourself. Two things to internalise before you write a query.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 17 days ago.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • vitaly-t/pg-promise3,548 ★ · -4
Star history
Repository2026-09-242026-11-25
vitaly-t/pg-promise3.6k3.5k

-4 Stars in the last 62 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The name describes a 2015 feature, not the library

The About section explains the name and then apologises for it. At its inception in 2015, the library was only adding promises to the base driver, hence the name. The original name was kept while the functionality was extended, with promises now being only its tiny part. What the library actually adds on top of node-postgres is a list of six things: automatic connections, automatic transactions, a query formatting engine together with query generation, a declarative approach to handling results, global events reporting for central handling, and support for external SQL files. So when you install it for promises you are installing a connection pool, a transaction scope, and an escaper, and the name will not tell you that.

The method name is the row count you expect back

Every query method is derived from one generic method, and the derived ones are named after how many rows of data the query is expected to return. There are five, none, one, oneOrNone, many, and manyOrNone, which is an alias of any. The documentation is emphatic about one thing: do not confuse the method name with the number of rows the query affects, which it calls completely irrelevant. The reason to use them is protection. If you call the singular method and the query returns two rows, the library rejects it and treats it as an error, so a query that starts returning more data than you assumed fails loudly instead of handing you an array of unexpected length. That turns a class of silent data bugs into a thrown error.

The generic query method borrows the connection every call

One warning is labelled important and it is about connections. The generic method, the one all the others are built on, acquires and releases the connection, which the documentation says makes it a poor choice for executing multiple queries at once. It therefore points at a chapter on chaining queries and calls it a must-read, so as to avoid writing the code that misuses connections. The methods it recommends learning first instead are task and the transaction pair, tx and its conditional form. Both expose what the documentation calls a connected protocol, which adds batch, page and sequence. That protocol is extensible through an event, so the surface a caller sees can be widened at runtime.

The formatter and template literals claim the same syntax

The query formatter is the part of this library that has to be understood before anything else, and the documentation carries two warnings about the same mistake. The first says never to use ES6 template strings or manual concatenation to generate queries, because both can easily result in broken queries, and that only this library's formatting engine knows how to escape values properly for PostgreSQL. The reason is visible in the syntax itself. Named parameters are written with a dollar sign, a property path, and a matching pair of delimiters, and one of the five available pairs is the curly brace form. That is exactly what JavaScript does inside a template literal, so a template string interpolates your parameter before the formatter ever runs. The second warning names the same trap and adds that inside a template literal only four of the five pairs work.

A bare value and a one-element array are not the same query

The classic formatting syntax injects values by position, from one up to a hundred thousand, and the example is a range query with two placeholders fed an array:

js
await db.any('SELECT * FROM product WHERE price BETWEEN $1 AND $2', [1, 10])

There is a second form for queries that use only the first placeholder, where the values argument is the value itself rather than an array. The documentation restricts it: it works only for the basic scalar types, and types like array and object change the way parameters are interpreted. The advice is to pass index variables inside an array anyway, as the safer option, to avoid ambiguity. So a call that looks equivalent to the array form is only equivalent for six types, and the failure mode is a different query rather than an error.

undefined becomes null, but a missing property throws

Two failure modes sit next to each other in the named parameters section and behave in opposite ways. A property whose value is null and a property whose value is undefined are both formatted as null, silently. A property that does not exist at all is a different case, and an error is thrown. The practical consequence is that a typo in a property path fails immediately while an accidentally undefined variable does not: it arrives at the database as null. That asymmetry is worth knowing about if you build queries from optional application state, because the first kind of mistake is caught and the second is not. The section also documents a self-reference, where the reserved name this resolves to the whole formatting object, inserted as a JSON string.

Every dependency is pinned to an exact version

Look at the manifest's version specifiers and there is not a single range operator in the file. The three runtime dependencies are the assertion helper at one patch, the pg driver at 8.23.0, a query minifier, and the author's own spec framework, each written as an exact number. The eleven development dependencies are pinned the same way, from the linter to the coverage tool to the TypeScript compiler. The peer dependency is the odd one: a single stream adapter pinned to exactly one patch version rather than a range, so a project on any other version of it sees a peer conflict. The engine floor is Node 16, and the published file list contains only the library directory and the type declarations, so the tests, the docs configuration and the lint setup never reach a consumer.

Lint covers two test paths and the suite needs a database

The scripts section is small and reveals how the project is checked. Linting runs the linter over the library directory, over the test files matched at the top level of the test directory, and over one named subdirectory of tests, so other test subdirectories are not covered by that command even though formatting does cover them. Tests run through a jasmine runner invoked directly, and the coverage script calls that runner by path inside node_modules rather than through the local binary, with an HTML and a text reporter. Two more scripts matter for anyone reproducing the run: one initialises a database by executing a script under the test directory, so the suite needs a live PostgreSQL, and the other runs the same tests with a configuration flag set to a name for native promises.

Editorial conclusion

pg-promise earns its place when you want the connection handling and the transaction scoping that the base driver leaves to you, and when you would rather hand SQL to a formatter than assemble strings yourself. Two things to internalise before you write a query. The method you choose declares how many rows you expect back, and a query that returns a different count becomes an error rather than an unexpected array. And the formatter has its own variable syntax that overlaps with JavaScript template literals, so a template string will interpolate your parameters before the library ever sees them. If you are already deep in the pg driver and comfortable managing connections yourself, the value here is mostly the formatter and the transaction scoping, not the promises in the name.

Frequently asked questions

What is pg-promise?

It is a PostgreSQL interface for Node.js built on top of node-postgres. On top of the driver it adds automatic connections, automatic transactions, a query formatting engine with query generation, a declarative approach to results, global events reporting and support for external SQL files. Its README says that when it started in 2015 it only added promises to the driver, and that promises are now a tiny part of it.

How does pg-promise compare to node-postgres?

It is a layer on top of it rather than a replacement: the pg driver is a runtime dependency, and the library adds connection handling, transactions, result handling and the formatter on top. One thing it does not replace is the discipline around connections, since the generic query method acquires and releases a connection per call and the documentation calls chaining queries a must-read.

Why should I not use a template literal for a pg-promise query?

Because the formatter's named parameter syntax and JavaScript's template literal syntax overlap. Named parameters are written with a dollar sign and a matching delimiter pair, and the curly brace pair is the reserved one, so a template string interpolates your values before the library formats them. The documentation warns twice, and notes that only four of the five available delimiter pairs work inside a template string.

What do the pg-promise query methods like one and many actually mean?

They name how many rows of data the query is expected to return, not how many rows it affects, which the documentation calls completely irrelevant. Using them protects you: a query that returns a different number of rows than the method promises is rejected and treated as an error rather than handed back as an unexpected array.

What does the pg-promise manifest require to install?

Node 16 or newer, three runtime dependencies pinned to exact versions including the pg driver at 8.23.0, and a peer dependency on a query stream adapter pinned to one exact patch version rather than a range. Only the library directory and the type declarations are published.

Official sources

  1. License: MIT
  2. Project website
  3. README
  4. Releases
  5. vitaly-t/pg-promise on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/vitaly-t-pg-promise.svg)](https://hysenlabs.com/projects/vitaly-t-pg-promise)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/vitaly-t-pg-promise.svg)](https://hysenlabs.com/projects/vitaly-t-pg-promise)

Was this useful?