Registry / database / pg-async

pg-async

JSON →
library3.1.0jsnpmunverified

pg-async (v3.1.0) is a tiny Promise-based PostgreSQL client for Node.js designed for easy use with ES7 async/await, wrapping the popular node-postgres (pg) library. It provides a concise API for queries, rows, values, and connection management, with support for tagged template literals via the SQL helper for safe parameterization. Released with an MIT license, it offers both pg and pg-native backends. Compared to raw pg or knex, pg-async reduces boilerplate while remaining lightweight and idiomatic for async/await patterns.

npm install pg-async
INSTALL
IMPORT
SIG · PG-ASYNC
P
pg-async
databasejavascriptv3.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.

PgAsync
import PgAsync from 'pg-async';
import { PgAsync } from 'pg-async';
PgAsync is the default export; named import fails.
SQL
import { SQL } from 'pg-async';
import SQL from 'pg-async';
SQL is a named export for tagged template literals.
require (CommonJS)
const { default: PgAsync, SQL } = require('pg-async');
const PgAsync = require('pg-async');
In CommonJS, the default export is under .default due to interop.

Basic operations: connect, create table, insert, query rows, single row, scalar value, and close.

import PgAsync, { SQL } from 'pg-async'; const pgAsync = new PgAsync({ host: process.env.PGHOST ?? 'localhost', port: parseInt(process.env.PGPORT ?? '5432'), user: process.env.PGUSER ?? 'postgres', password: process.env.PGPASSWORD ?? '', database: process.env.PGDATABASE ?? 'test', }); async function main() { // Create a table await pgAsync.query(SQL` CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE NOT NULL ) `); // Insert a row await pgAsync.query(SQL` INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com') `); // Query rows as array of objects const rows = await pgAsync.rows(SQL`SELECT * FROM users`); console.log(rows); // [{ id: 1, name: 'Alice', email: 'alice@example.com' }] // Use parameterized query with tagged template const email = 'alice@example.com'; const user = await pgAsync.row(SQL`SELECT * FROM users WHERE email = ${email}`); console.log(user); // Get a single scalar value const count = await pgAsync.value(SQL`SELECT count(*) FROM users`); console.log('Count:', count); // Close connection await pgAsync.end(); } main().catch(console.error);
Debug
Known issues
gotchapg-async relies on the pg package; installing pg is required even if using pg-native.
fix
npm install pg (and optionally pg-native)
affects: all
breakingWhen using CommonJS require, the default export is under .default property.
fix
Use const PgAsync = require('pg-async').default;
affects: >=3.0.0
deprecatedUsing new PgAsync(null, 'native') to select the native driver is deprecated in favor of passing a driver object.
fix
Use new PgAsync(null, require('pg').native);
affects: >=3.0.0
gotchaThe database connection will not be established until the first query; errors like invalid credentials appear only on first query.
fix
Call a dummy query like pgAsync.query(SQL`SELECT 1`) to verify connection early.
affects: all
gotchaWhen using pg-native, the package must be installed separately and the pg package's native property must be used as driver.
fix
Install pg-native and use new PgAsync(null, require('pg').native);
affects: all
Errors
Common errors & fixes
TypeError: PgAsync is not a constructor
Using named import instead of default import in ESM, or incorrect CommonJS require without .default
fix
import PgAsync from 'pg-async'; (ESM) or const PgAsync = require('pg-async').default; (CJS)
Cannot read properties of undefined (reading 'query')
Forgot to instantiate PgAsync class, i.e., using PgAsync.query(...) instead of const db = new PgAsync(); db.query(...)
fix
Create an instance: const pgAsync = new PgAsync(); await pgAsync.query(...)
Error: pg-native is not installed
Attempted to use native driver without installing pg-native, or passing 'native' string which is deprecated
fix
Install pg-native and use new PgAsync(null, require('pg').native)
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies
pgrequiredCore PostgreSQL driver providing the underlying client and query execution
Agent activity
2 hits · last 30 days
node
2
Resources
pg-async — npm install pg-async · libregistry