pg-connection-string
Functions for dealing with a PostgresSQL connection string
Last updated 2 years ago by brianc .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install pg-connection-string 
SYNC missed versions from official npm registry.

pg-connection-string

NPM

Functions for dealing with a PostgreSQL connection string

parse method taken from node-postgres Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) MIT License

Usage

const parse = require('pg-connection-string').parse;

const config = parse('postgres://someuser:somepassword@somehost:381/somedatabase')

The resulting config contains a subset of the following properties:

  • user - User with which to authenticate to the server
  • password - Corresponding password
  • host - Postgres server hostname or, for UNIX domain sockets, the socket filename
  • port - port on which to connect
  • database - Database name within the server
  • client_encoding - string encoding the client will use
  • ssl, either a boolean or an object with properties
    • rejectUnauthorized
    • cert
    • key
    • ca
  • any other query parameters (for example, application_name) are preserved intact.

ClientConfig Compatibility for TypeScript

The pg-connection-string ConnectionOptions interface is not compatible with the ClientConfig interface that pg.Client expects. To remedy this, use the parseIntoClientConfig function instead of parse:

import { ClientConfig } from 'pg';
import { parseIntoClientConfig } from 'pg-connection-string';

const config: ClientConfig = parseIntoClientConfig('postgres://someuser:somepassword@somehost:381/somedatabase')

You can also use toClientConfig to convert an existing ConnectionOptions interface into a ClientConfig interface:

import { ClientConfig } from 'pg';
import { parse, toClientConfig } from 'pg-connection-string';

const config = parse('postgres://someuser:somepassword@somehost:381/somedatabase')
const clientConfig: ClientConfig = toClientConfig(config)

Connection Strings

The short summary of acceptable URLs is:

  • socket:<path>?<query> - UNIX domain socket
  • postgres://<user>:<password>@<host>:<port>/<database>?<query> - TCP connection

But see below for more details.

UNIX Domain Sockets

When user and password are not given, the socket path follows socket:, as in socket:/var/run/pgsql. This form can be shortened to just a path: /var/run/pgsql.

When user and password are given, they are included in the typical URL positions, with an empty host, as in socket://user:pass@/var/run/pgsql.

Query parameters follow a ? character, including the following special query parameters:

  • db=<database> - sets the database name (urlencoded)
  • encoding=<encoding> - sets the client_encoding property

TCP Connections

TCP connections to the Postgres server are indicated with pg: or postgres: schemes (in fact, any scheme but socket: is accepted). If username and password are included, they should be urlencoded. The database name, however, should not be urlencoded.

Query parameters follow a ? character, including the following special query parameters:

  • host=<host> - sets host property, overriding the URL's host
  • encoding=<encoding> - sets the client_encoding property
  • ssl=1, ssl=true, ssl=0, ssl=false - sets ssl to true or false, accordingly
  • uselibpqcompat=true - use libpq semantics
  • sslmode=<sslmode> when uselibpqcompat=true is not set
    • sslmode=disable - sets ssl to false
    • sslmode=no-verify - sets ssl to { rejectUnauthorized: false }
    • sslmode=prefer, sslmode=require, sslmode=verify-ca, sslmode=verify-full - sets ssl to true
  • sslmode=<sslmode> when uselibpqcompat=true
    • sslmode=disable - sets ssl to false
    • sslmode=prefer - sets ssl to { rejectUnauthorized: false }
    • sslmode=require - sets ssl to { rejectUnauthorized: false } unless sslrootcert is specified, in which case it behaves like verify-ca
    • sslmode=verify-ca - sets ssl to { checkServerIdentity: no-op } (verify CA, but not server identity). This verifies the presented certificate against the effective CA specified in sslrootcert.
    • sslmode=verify-full - sets ssl to {} (verify CA and server identity)
  • sslcert=<filename> - reads data from the given file and includes the result as ssl.cert
  • sslkey=<filename> - reads data from the given file and includes the result as ssl.key
  • sslrootcert=<filename> - reads data from the given file and includes the result as ssl.ca

A bare relative URL, such as salesdata, will indicate a database name while leaving other properties empty.

[!CAUTION] Choosing an sslmode other than verify-full has serious security implications. Please read https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS to understand the trade-offs.

Current Tags

  • 2.7.1-alpha.0                                ...           beta (2 years ago)
  • 2.14.1                                ...           latest (2 months ago)

33 Versions

  • 2.14.1                                ...           2 months ago
  • 2.14.0                                ...           5 months ago
  • 2.13.0                                ...           6 months ago
  • 2.12.0                                ...           9 months ago
  • 2.11.0                                ...           10 months ago
  • 2.10.1                                ...           10 months ago
  • 2.10.0                                ...           10 months ago
  • 2.9.1                                ...           a year ago
  • 2.9.0                                ...           2 years ago
  • 2.8.5                                ...           2 years ago
  • 2.8.2                                ...           2 years ago
  • 2.8.1                                ...           2 years ago
  • 2.8.0                                ...           2 years ago
  • 2.7.1-alpha.0                                ...           2 years ago
  • 2.7.0                                ...           2 years ago
  • 2.6.4                                ...           3 years ago
  • 2.6.3                                ...           3 years ago
  • 2.6.2                                ...           3 years ago
  • 2.6.1                                ...           3 years ago
  • 2.6.0                                ...           4 years ago
  • 2.5.0                                ...           6 years ago
  • 2.4.0                                ...           6 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.3                                ...           7 years ago
  • 2.2.2                                ...           7 years ago
  • 2.2.1                                ...           7 years ago
  • 2.2.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.0                                ...           9 years ago
  • 0.1.3                                ...           12 years ago
  • 0.1.2                                ...           12 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
Downloads
Total 123
Today 1
This Week 8
This Month 67
Last Day 1
Last Week 7
Last Month 36
Dependencies (0)
None
Dev Dependencies (4)
Dependents (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE