-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres-pool.js
48 lines (41 loc) · 1.51 KB
/
postgres-pool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { Pool } = require("pg");
const DB_CONNECTION = process.env.DB_CONNECTION;
// Set up the connection string
let cs = {
connectionString:
process.env.DB_CONNECTION + "?application_name=centurionV2-workers",
max: process.env.DB_MAX_CONNECTIONS,
idleTimeoutMillis: process.env.DB_IDLE_TIMEOUT_MILLIS, // default is 10 seconds
connectionTimeoutMillis: process.env.DB_CONNECTION_TIMEOUT_MILLIS, // default is 0 or no timeout
};
// Create the pool
const pool = new Pool(cs);
pool.on("error", (err, client) => {
debug("> POSTGRESQL Unexpected error on idle client", err);
try {
loki.postLokiError(
err,
err.toString(),
'Postgres: pool.on("error"), consider restart',
);
} catch (err) {
console.error(err);
}
});
async function test() {
console.log("| DB_CONNECTION:", DB_CONNECTION);
console.log("| max:", cs.max);
console.log("| idleTimeoutMillis:", cs.idleTimeoutMillis);
console.log("| connectionTimeoutMillis:", cs.connectionTimeoutMillis);
const client = await pool.connect();
console.log(`| Transactional processID ${client.processID}`);
const res = await client.query("SELECT NOW()");
client.release();
const pgPool = await pool.query("SELECT NOW()");
console.log(`| Non transactional processID ${client.processID}`);
console.log(`| processIDs (a.k.a connectionIDs) should match`);
console.log(`| Pool count at startup: ${pool.totalCount}`);
console.info("|----------------------------------------------------|");
}
test();
module.exports = pool;