Fix postgresql connection leaks

Return connection to pool on connection error (see #1708)
Fix leaks on persist_connection=false (#1711)

Includes testcase for #1711

This is a squashed rebase of #1707
\cc @springmeyer