Fix postgresql connection leaks on error

See #1708