Package Exports
- pg-env
- pg-env/esm/index.js
- pg-env/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pg-env) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
pg-env
PostgreSQL environment configuration utilities for managing database connection settings.
Installation
npm install pg-envFeatures
- Parse PostgreSQL environment variables (PGHOST, PGPORT, etc.)
- Convert configuration objects to environment variables
- Merge environment variables with configuration overrides
- Create spawn environments with PostgreSQL settings
Usage
Basic Configuration
import { PgConfig, getPgEnvOptions } from 'pg-env';
// Get PostgreSQL config from environment with defaults
const config = getPgEnvOptions();
console.log(config);
// { host: 'localhost', port: 5432, user: 'postgres', ... }
// Override specific values
const customConfig = getPgEnvOptions({
database: 'myapp',
port: 5433
});Environment Variables
import { getPgEnvVars, toPgEnvVars } from 'pg-env';
// Read current PostgreSQL environment variables
const envVars = getPgEnvVars();
// Returns partial PgConfig from PGHOST, PGPORT, etc.
// Convert config to environment variables
const config: PgConfig = {
host: 'db.example.com',
port: 5432,
user: 'appuser',
password: 'secret',
database: 'myapp'
};
const envVars = toPgEnvVars(config);
// { PGHOST: 'db.example.com', PGPORT: '5432', ... }Spawning Processes
import { getSpawnEnvWithPg } from 'pg-env';
import { spawn } from 'child_process';
// Create environment for spawning processes
const env = getSpawnEnvWithPg({
database: 'testdb',
user: 'testuser'
});
// Use with child processes
const child = spawn('psql', [], { env });API
Types
PgConfig
interface PgConfig {
host: string;
port: number;
user: string;
password: string;
database: string;
}Functions
getPgEnvOptions(overrides?: Partial<PgConfig>): PgConfig- Get config from environment with overridesgetPgEnvVars(): Partial<PgConfig>- Parse PostgreSQL environment variablestoPgEnvVars(config: Partial<PgConfig>): Record<string, string>- Convert config to env varsgetSpawnEnvWithPg(config: Partial<PgConfig>, baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv- Create spawn environment
Constants
defaultPgConfig: PgConfig- Default PostgreSQL configuration
Environment Variables
The package reads the following environment variables:
PGHOST- Database hostPGPORT- Database port (parsed as number)PGUSER- Database userPGPASSWORD- Database passwordPGDATABASE- Database name