psycopg2 connection already closed In one of our environments we execute quite heavy query from python script using psycopg2. It takes more then 10 min to execute but we get a result and can consume received data.

When we move the same script to different environment then after ~5min we get an error: psycopg2.InterfaceError: connection already closed

What may be the reason for that?
The most likely reason is you have a firewall or gateway which is not very patient and is dropping the connection after 5 minutes.  Usually when this happens, it makes it look to the client like the server closed the connection, and to the server like the client closed the connection.  That seems to be compatible with the error message you see in python.  What does the server's log file blame the problem on?