Add `onConnect` callback which allows "setup" of a pooled client by brianc · Pull Request #3620 · brianc/node-postgres

This allows you to do

const pool = new Pool({
  onConnect: async (client) => await client.query('SET search_path=foo,public')
})

for example.

If the onConnect function throws or rejects the error will be sent out to the callsite of pool.connect or pool.query & the client will be disposed of. This only triggers when the client is first connected to postgres not when an existing connected client is checked out.

Why did I call it pool.connect so long ago instead of pool.getClient() or pool.aquire() or something? I don't know. All I know is I'm not very fond of myself from 10 years ago. Oh well! I'll make sure to include good documentation on this change before I actually merge it but want to put it up for some 👀

This should fix #3617

I omitted the other hook ideas I had as I need to think through them more, and in the case of onClose the release method is sync, and some times clients are closed in the background so error handling there is a different issue I'll need to think about.