exit_nicely() calls PQfinish(conn);It needs still to be called.Only the exit(1) is the erroneous call.
Marko Ristola