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

Cheok Yan Cheng picture Cheok Yan Cheng · Feb 12, 2018 · Viewed 19.5k times · Source

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?

Answer

user268396 picture user268396 · Feb 12, 2018

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.)