PostgreSQL Wrapper for database-js
database-js-postgres is a wrapper library designed to integrate PostgreSQL databases with the database-js framework. It utilizes the widely adopted node-postgres package under the hood to handle core database interactions, exposing its functionality through a consistent promise-based API, even when used independently of database-js. The library reached its latest stable version, 1.1.3, in late 2017. Given the lack of updates or commits since then, its release cadence is effectively ceased, and the project is considered unmaintained. Its primary differentiation was to provide a standardized interface within the database-js ecosystem, enabling a uniform approach to various SQL databases. However, its dependency on outdated Node.js versions and its CommonJS-only structure limit its applicability in modern JavaScript environments.
Common errors
-
Error: connect ECONNREFUSED
cause The PostgreSQL server is not running, is not accessible from the application's host, or the specified port is incorrect.fixVerify that your PostgreSQL server is running, listening on the specified host and port (default 5432), and that no firewall is blocking the connection. -
Error: password authentication failed for user "my_secret_username"
cause Incorrect username or password, or the PostgreSQL server's `pg_hba.conf` configuration does not allow the specified authentication method for the user/database.fixDouble-check the username and password in your connection string. Consult your PostgreSQL server's `pg_hba.conf` file to ensure the client IP address and user are permitted to connect with password authentication (e.g., `md5`, `scram-sha-256`). -
Cannot find module 'database-js-postgres'
cause The package is not installed or `npm install` was not run.fixRun `npm install database-js-postgres` in your project directory to install the package.
Warnings
- breaking The package `database-js-postgres` has been abandoned since late 2017. It no longer receives updates, bug fixes, or security patches. Using it in production is highly discouraged.
- gotcha The documentation specifies a prerequisite of 'NodeJS >= 6.0'. Modern Node.js versions are not guaranteed to be compatible, and using such an old Node.js version is a significant security risk.
- gotcha This package is CommonJS-only and does not support ES Modules (ESM) syntax (`import/export`). Attempting to use ESM will result in errors.
- gotcha When used with `database-js`, the README explicitly imports from `database-js2`. Ensure the correct package (`database-js` or `database-js2`) is installed and matched to the example's usage, as this can be a source of confusion.
Install
-
npm install database-js-postgres -
yarn add database-js-postgres -
pnpm add database-js-postgres
Imports
- postgres
import postgres from 'database-js-postgres';
const postgres = require('database-js-postgres'); - Connection
import { Connection } from 'database-js2';const Database = require('database-js2').Connection; - Connection (standalone)
import { open } from 'database-js-postgres'; const connection = open({ ... });const postgres = require('database-js-postgres'); const connection = postgres.open({ ... });
Quickstart
const postgres = require('database-js-postgres');
(async () => {
let connection, rows;
// Establish a connection to the PostgreSQL database
connection = postgres.open({
Hostname: 'localhost',
Port: 5432,
Username: 'my_secret_username',
Password: 'my_secret_password',
Database: 'my_top_secret_database'
});
try {
// Execute a query and fetch results
rows = await connection.query("SELECT id, name FROM users WHERE email = ?", ['example@email.com']);
console.log('Query Results:', rows);
// Example of an insert statement
const insertResult = await connection.query("INSERT INTO logs (message) VALUES (?) RETURNING id", ['User logged in successfully']);
console.log('Insert Result:', insertResult);
} catch (error) {
console.error('Database Operation Error:', error);
} finally {
// Always ensure the connection is closed
await connection.close();
console.log('Connection closed.');
}
})();