Usually, when your python program exits, all its sockets are closed and transactions are aborted. But it is good practice to close the connection at the very end.
Closing a connection as soon as you no longer need it frees up system resources. This is always good.
Keep in mind that if you close your connection, make your changes first. As you can read in the psycopg2 API:
Close the connection now (and not whenever del is executed). The connection will be unsuitable from this point forward; when any connection operation occurs, an InterfaceError will be raised. The same applies to all cursor objects trying to use a connection. Note that closing a connection without making a change first will discard any pending changes, as if ROLLBACK were running
Djanssens
source share