postgresql plugin: Improved connection handling

Hi,

this has been sitting around for a while; apparently, I forgot to send a PR in the past ;-)

Cheers,
Sebastian