Installing

$ npm install pg-promise

Initialization

Loading and initializing the library with Initialization Options:

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

− or without Initialization Options:

const pgp = require('pg-promise')();

Database

Create your Database object from the connection details:

const db = pgp(connection);

The connection parameter is either a Configuration Object or a Connection String.

Object db represents the Database protocol with lazy connection, i.e. only the actual query methods acquire and release the connection automatically. Therefore you should create only one global/shared db object per connection details.

It is best to initialize the library and create Database in its own module, see Where should I initialize pg-promise.

Usage

Learn by Example is the best quick-start tutorial. For everything else see the WiKi pages.

References

External Resources

Main Types

  • Database - database-level methods and properties
  • Task - extends Database with task-level methods and properties

Special Types

Namespaces

  • formatting - the library's query formatting engine
  • helpers - for automatic query generation
  • utils - simplifies the use of external SQL files
  • errors - custom error types supported by the library
  • txMode - types for configuring a transaction

Events

  • connect - connecting to the database
  • disconnect - disconnecting from the database
  • query - executing a query
  • task - task start/end events
  • transact - transaction start/end events
  • receive - receiving data from a query
  • error - global error handler
  • extend - interface extension event