postgres
PostgreSQL client for node.js.
Install
$ npm i node-postgres
Status
Useage
Client
const { Client } = require('node-postgres');
(async () => {
const client = new Client({
user: 'postgres',
host: '127.0.0.1',
database: 'test',
password: 'esri@123',
port: 5432
});
await client.connect();
const res = await client.query('SELECT * from users');
console.log(res);
await client.end();
})().catch(console.error);
ssl
const { Client, Pool } = require('node-postgres');
const fs = require('fs');
(async () => {
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'test',
password: 'esri@123',
port: 5432,
ssl: {
rejectUnauthorized: false,
key: fs.readFileSync('c:/my/server.key').toString(),
cert: fs.readFileSync('c:/my/server.crt').toString(),
}
});
await client.connect();
const res = await client.query('SELECT * from users');
console.log(res);
await client.end();
})().catch(console.error);
Pool
The client pool allows you to have a reusable pool of clients you can check out, use, and return. You generally want a limited number of these in your application and usually just 1. Creating an unbounded number of pools defeats the purpose of pooling at all.
Checkout, use, and return
const { Pool } = require('node-postgres');
(async () => {
const pool = new Pool({
user: 'postgres',
host: '127.0.0.1',
database: 'test',
password: 'esri@123',
port: 5432
});
const client = await pool.connect();
try {
const res = await client.query('SELECT * from users');
console.log(res);
} catch (error) {
console.log(error);
} finally {
client.release();
}
})().catch(console.error);
You must always return the client to the pool if you successfully check it out, regardless of whether or not there was an error with the queries you ran on the client. If you don't check in the client your application will leak them and eventually your pool will be empty forever and all future requests to check out a client from the pool will wait forever.
Single query
If you don't need a transaction or you just need to run a single query, the pool has a convenience method to run a query on any available client in the pool. This is the preferred way to query with node-postgres if you can as it removes the risk of leaking a client.
const { Pool } = require('node-postgres');
(async () => {
const pool = new Pool({
user: 'postgres',
host: '127.0.0.1',
database: 'test',
password: 'esri@123',
port: 5432
});
const res = await pool.query('SELECT * from users');
console.log(res);
})().catch(console.error);
Transation
To execute a transaction with node-postgres you simply execute BEGIN / COMMIT / ROLLBACK queries yourself through a client. Because node-postgres strives to be low level and un-opinionated, it doesn't provide any higher level abstractions specifically around transactions.
You must use the same client instance for all statements within a transaction. PostgreSQL isolates a transaction to individual clients. This means if you initialize or use transactions with the pool.query method you will have problems. Do not use transactions with the pool.query method.
const { Pool } = require('node-postgres');
const pool = new Pool({
user: 'postgres',
host: '127.0.0.1',
database: 'test',
password: 'esri@123',
port: 5432
});
(async () => {
const client = await pool.connect()
try {
await client.query('BEGIN')
await client.query(`INSERT INTO users(id, name) VALUES(1, 'zfx')`);
await client.query(`INSERT INTO users(id, name) VALUES(2, 'zfx2')`);
await client.query(`DELETE FROM users WHERE id=0`);
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK')
throw e
} finally {
client.release()
}
})().catch(console.error);
Shutdown
To shut down a pool call pool.end() on the pool. This will wait for all checked-out clients to be returned and then shut down all the clients and the pool timers.
const { Pool } = require('node-postgres');
(async () => {
const pool = new Pool({
user: 'postgres',
host: '127.0.0.1',
database: 'test',
password: 'esri@123',
port: 5432
});
const res = await pool.query('SELECT * from users');
console.log(res);
await pool.end()
await pool.query('SELECT * from users');
})().catch(console.error);
The pool will return errors when attempting to check out a client after you've called pool.end()
on the pool.
constructor
client constructor options
import http = require('http');
import https = require('https');
interface Options {
user: string,
password: string,
database: string,
port: number,
ssl?: any,
connectionTimeoutMillis?: int,
}
pool constructor options
Every field of the config object is entirely optional. The config passed to the pool is also passed to every client instance within the pool when the pool creates that client.
interface Options {
idleTimeoutMillis?: int,
max?: int,
waitForConnections?: boolean,
waitForConnectionsMillis?: int,
queueLimit?: int,
}
Test
$ npm test