added error handling when closing pg cursor