Skip to content

charmander/libpq-connection-string

Repository files navigation

A libpq-compatible* connection string parser, like PQconninfoParse.

const parseConnectionString = require('libpq-connection-string');

parseConnectionString('postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp')
// { … dbname: 'somedb', host: 'host1,host2', port: '123,456' … }

parseConnectionString('host=localhost port=5432 dbname=mydb connect_timeout=10')
// { … connect_timeout: '10', dbname: 'mydb', host: 'localhost', port: '5432' … }

All provided option values are parsed as strings, and all non-provided option values are null.

Service files

libpq-connection-string/add-service-defaults-sync is a function to read service configuration synchronously.

LDAP isn’t supported.

Environment defaults

libpq-connection-string/add-env-defaults is a function to add libpq defaults (excluding those that come from service files) to parsed options. It reads from:

  • environment variables
  • the current user’s username, if a user isn’t otherwise specified
  • a static set of fallback defaults

Examples

Getting the defaults that PQconnect would use:

const parseConnectionString = require('libpq-connection-string');
const addServiceDefaultsSync = require('libpq-connection-string/add-service-defaults-sync');
const addEnvDefaults = require('libpq-connection-string/add-env-defaults');

const options = parseConnectionString('host=/run/postgres');
addServiceDefaultsSync(options);
addEnvDefaults(options);

* when the connection string is normally encoded as UTF-8 and the C locale is used for isspace

Releases

No releases published

Packages

No packages published