How can I choose between Client or Pool for node-postgres

node.jsNode Postgres

node.js Problem Overview


From https://node-postgres.com/features/connecting , seems like we can choose between Pool or Client to perform query

pool.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

client.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  client.end()
})

Their functionalities look very much the same. But, the documentation doesn't explain much the difference between Pool and Client.

May I know, what thing I should consider, before choosing between Pool or Client?

node.js Solutions


Solution 1 - node.js

> May I know, what thing I should consider, before choosing between Pool or Client?

Use a pool if you have or expect to have multiple concurrent requests. That is literally what it is there for: to provide a pool of re-usable open client instances (reduces latency whenever a client can be reused).

In that case you definitely do not want to call pool.end() when your query completes, you want to reserve that for when your application terminates because pool.end() disposes of all the open client instances. (Remember, the point is to keep up to a fixed number of client instances available.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCheok Yan ChengView Question on Stackoverflow
Solution 1 - node.jsuser268396View Answer on Stackoverflow