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, and should be avoided. Instead, you should pass those parameters within the connection details.

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. Check the driver for their usage.

Example:

const cn = {
    host: 'localhost',
    port: 5432,
    database: 'my-database-name',
    user: 'user-name',
    password: 'user-password'
};

const db = pgp(cn);

Connection String

Connection strings support only a subset of connection parameters as show below:

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

Other parameters are also supported, similar to the ones within the connection object, but not all of them. For example, when ssl needs to be an object, it is not supported by the connection string.

Example:

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

See also:

Clone this wiki locally