Skip to content

Connection Syntax

Vitaly Tomilov edited this page Mar 9, 2020 · 101 revisions

Connection details syntax to be used when creating a Database object.

Most of the connection parameters are optional. See their default values. Please note that overriding defaults via pg.defaults doesn't not work for all parameters (see #699), and should be avoided. Instead, you should pass those parameters within the connection details (configuration object).

Configuration Object

  • string host - server name or IP address
  • number port - server port number
  • string database - database name
  • string user - user name
  • string password - user password, or a function that returns one
  • boolean ssl - use SSL (it also can be an ISSLConfig-like object)
  • boolean binary - binary result mode
  • string client_encoding
  • string application_name
  • string fallback_application_name
  • number idleTimeoutMillis - lifespan for unused connections
  • number max - maximum size of the connection pool
  • number query_timeout - query execution timeout
  • boolean keepAlive - keep TCP alive

etc, there are more advanced parameters - reapIntervalMillis, returnToHead, poolLog, parseInputDatesAsUTC, rows and statement_timeout. You can check the driver for their usage.

Example:

const cn = {
    host: 'localhost',
    port: 5432,
    database: 'my-database-name',
    user: 'user-name',
    password: 'user-password',
    max: 30 // use up to 30 connections
};

const db = pgp(cn);

Connection String

General syntax for the connection string is as shown below:

postgres://username:password@host:port/database?ssl=false&application_name=name
&fallback_application_name=name&client_encoding=encoding

However, it is limited as to what parameters can be passed in, as per pg-connection-string, used by the driver internally, when you pass in a connection string directly.

You can use the generic connection-string instead, to let it parse any parameters you want, and then generate your configuration object from those, as it shows in the examples.

Example:

const db = pgp('postgres://john:pass123@localhost:5432/products');

See also:

Clone this wiki locally