
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
pg-connection-string
Advanced tools
Functions for dealing with a PostgresSQL connection string
parse method taken from node-postgres
Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
MIT License
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 serverpassword - Corresponding passwordhost - Postgres server hostname or, for UNIX domain sockets, the socket filenameport - port on which to connectdatabase - Database name within the serverclient_encoding - string encoding the client will usessl, either a boolean or an object with properties
rejectUnauthorizedcertkeycaapplication_name) are preserved intact.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)
The short summary of acceptable URLs is:
socket:<path>?<query> - UNIX domain socketpostgres://<user>:<password>@<host>:<port>/<database>?<query> - TCP connectionBut see below for more details.
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 propertyTCP 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 hostencoding=<encoding> - sets the client_encoding propertyssl=1, ssl=true, ssl=0, ssl=false - sets ssl to true or false, accordinglyuselibpqcompat=true - use libpq semanticssslmode=<sslmode> when uselibpqcompat=true is not set
sslmode=disable - sets ssl to falsesslmode=no-verify - sets ssl to { rejectUnauthorized: false }sslmode=prefer, sslmode=require, sslmode=verify-ca, sslmode=verify-full - sets ssl to truesslmode=<sslmode> when uselibpqcompat=true
sslmode=disable - sets ssl to falsesslmode=prefer - sets ssl to { rejectUnauthorized: false }sslmode=require - sets ssl to { rejectUnauthorized: false } unless sslrootcert is specified, in which case it behaves like verify-casslmode=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.certsslkey=<filename> - reads data from the given file and includes the result as ssl.keysslrootcert=<filename> - reads data from the given file and includes the result as ssl.caA 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.
The connection-string package is a more generic parser for connection strings, not limited to PostgreSQL. It can parse various types of connection strings into a structured format. Compared to pg-connection-string, it offers a broader scope of functionality but might not have PostgreSQL-specific optimizations.
FAQs
Functions for dealing with a PostgresSQL connection string
The npm package pg-connection-string receives a total of 8,801,435 weekly downloads. As such, pg-connection-string popularity was classified as popular.
We found that pg-connection-string demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.