postgres: close connection when cursor can't be opened (fixes #10119)