PostgreSQL Connection URL Builder

0.1.0 · abandoned · verified Wed Apr 22

pg-database-url is a minimalist utility that constructs a PostgreSQL connection string (URI) from a structured JavaScript configuration object. Released as version 0.1.0, this package targets older Node.js environments (specifically `>=4.2.0`) and exclusively uses CommonJS modules. As of the last observed activity, the project appears to be abandoned, with no significant updates or maintenance in recent years (last commit over 11 years ago). Its primary function is to simplify the creation of connection strings for use with PostgreSQL client libraries like `node-postgres`, converting host, port, database, username, and password into a standard `postgres://` URI format. It does not offer advanced features like connection pooling, query building, or extensive validation, focusing solely on the URI generation aspect. There are no clear release cadences or stated differentiators, as it is a very specific, single-purpose library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a PostgreSQL connection string from a configuration object, suitable for use with client libraries like `node-postgres`. It includes placeholder environment variables for security and best practices.

const pgUrl = require('pg-database-url');

// Basic configuration from environment variables or a config file
const dbConfig = {
  host: process.env.DB_HOST ?? 'localhost',
  port: parseInt(process.env.DB_PORT ?? '5432', 10),
  database: process.env.DB_NAME ?? 'mydb',
  username: process.env.DB_USER ?? 'user',
  password: process.env.DB_PASSWORD ?? 'pass'
};

const connectionString = pgUrl(dbConfig);
console.log('Generated Connection String:', connectionString);

// Example with `node-postgres` (install separately: npm install pg)
// const { Client } = require('pg');
// const client = new Client({ connectionString });
//
// async function testConnection() {
//   try {
//     await client.connect();
//     const res = await client.query('SELECT NOW() as current_time');
//     console.log('Database time:', res.rows[0].current_time);
//   } catch (err) {
//     console.error('Database connection error:', err);
//   } finally {
//     await client.end();
//   }
// }
//
// testConnection();

view raw JSON →