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);
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
fiximport 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(...)
fixCreate 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
fixInstall pg-native and use new PgAsync(null, require('pg').native) Audit
Dependencies
pgrequiredCore PostgreSQL driver providing the underlying client and query execution