postgres
PostgreSQL client for node.js.
Install
$ npm i node-postgres
Status
- client
- pool
- ssl
- end
- transactions
Useage
Client
const Client = ; async { const client = user: 'postgres' host: '127.0.0.1' database: 'test' password: 'esri@123' port: 5432 ; await client; const res = await client; console; await client;};
ssl
const Client Pool = ;const fs = ; async { const client = user: 'postgres' host: 'localhost' database: 'test' password: 'esri@123' port: 5432 ssl: rejectUnauthorized: false // ca: fs.readFileSync('c:/my/server.crt').toString(), key: fs cert: fs ; await client; const res = await client; console; await client;};
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 = ; async { const pool = user: 'postgres' host: '127.0.0.1' database: 'test' password: 'esri@123' port: 5432 ; const client = await pool; try const res = await client; console; catch error console; finally client; };
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 = ; async { const pool = user: 'postgres' host: '127.0.0.1' database: 'test' password: 'esri@123' port: 5432 ; const res = await pool; console;};
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 = ;const pool = user: 'postgres' host: '127.0.0.1' database: 'test' password: 'esri@123' port: 5432; async { // note: we don't try/catch this because if connecting throws an exception // we don't need to dispose of the client (it will be undefined) const client = await pool try await client await client; await client; await client; await client; catch e await client throw e finally client };
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 = ; async { const pool = user: 'postgres' host: '127.0.0.1' database: 'test' password: 'esri@123' port: 5432 ; const res = await pool; console; await pool // will throw error await pool;};
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
;;
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.
Test
$ npm test