Registry / database / pg-connection-from-env

pg-connection-from-env

JSON →
library1.1.0jsnpmunverified

Utility library to parse PostgreSQL connection credentials from environment variables (POSTGRES_HOST, POSTGRES_PASS, etc.) into a configuration object or connection URI string. Current stable version is 1.1.0, released as a minimal zero-dependency TypeScript package. Unlike manual parsing, it handles multiple env var naming conventions, supports optional fallback defaults, and returns a typed object compatible with libraries like 'pg' or 'node-postgres'. Ships with TypeScript declarations and is ESM/CJS compatible.

npm install pg-connection-from-env
INSTALL
IMPORT
SIG · PG-CONNECTION-FROM
P
pg-connection-from-env
databasejavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (getPgConnectionFromEnv)
import getPgConnectionFromEnv from 'pg-connection-from-env'
const getPgConnectionFromEnv = require('pg-connection-from-env')
Default export is a named function; named import also works: import { getPgConnectionFromEnv } from 'pg-connection-from-env'
getConnectionStringFromEnv
import { getConnectionStringFromEnv } from 'pg-connection-from-env'
import getConnectionStringFromEnv from 'pg-connection-from-env'
Must use named import, not default import.
PgConnectionFromEnvOptions
import type { PgConnectionFromEnvOptions } from 'pg-connection-from-env'
import { PgConnectionFromEnvOptions } from 'pg-connection-from-env'
Type import helps avoid runtime inclusion. Type exists since v1.0.0.

Shows default and custom env usage to get a connection config object.

import getPgConnectionFromEnv from 'pg-connection-from-env'; // Use process.env (default) const conn = getPgConnectionFromEnv(); console.log(conn); // { host: 'localhost', user: 'postgres', port: 5432, password: '', database: 'postgres', ssl: false } // Custom env and fallback defaults const customEnv = { POSTGRES_HOST: 'mydb.example.com', POSTGRES_USER: 'admin', POSTGRES_PASSWORD: 'secret', POSTGRES_DATABASE: 'myapp', POSTGRES_PORT: '5432', }; const conn2 = getPgConnectionFromEnv({ env: customEnv }); console.log(conn2); // { host: 'mydb.example.com', user: 'admin', port: 5432, password: 'secret', database: 'myapp', ssl: false }
Debug
Known issues
gotchaThe environment variable POSTGRES_SSL is checked for truthiness (any non-empty string yields true). Setting POSTGRES_SSL=false will still enable SSL because non-empty string is truthy.
fix
Do not set POSTGRES_SSL to 'false'; leave it unset or set to empty string to disable SSL.
affects: >=0.1.0
gotchaIf both POSTGRES_URI and individual host/user/etc. env vars are set, the URI takes precedence, and other vars are ignored. This can be unexpected if you set both accidentally.
fix
Use only URI env var or only individual vars, not both. Or use fallback defaults with explicit override options.
affects: >=1.0.0
gotchaThe port is returned as a number, but if not set, it defaults to 5432. If an invalid non-numeric string is provided, it will fall back to 5432 silently.
fix
Ensure POSTGRES_PORT is a valid numeric string or leave it unset.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: getPgConnectionFromEnv is not a function
Using default import when the package was already imported as a named export mismatch in CJS contexts.
fix
Use named import: import { getPgConnectionFromEnv } from 'pg-connection-from-env'
Cannot find module 'pg-connection-from-env' or its corresponding type declarations.
Package not installed or missing from node_modules.
fix
Run: npm install pg-connection-from-env
TS2305: Module '"pg-connection-from-env"' has no exported member 'PgConnectionFromEnvOptions'.
Trying to import a type that may not exist in older versions (<1.0.0).
fix
Update to version 1.0.0 or later: npm install pg-connection-from-env@latest
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
pg-connection-from-env — npm install pg-connection-from-env · libregistry