conn.autocommit = True conn.set_client_encoding('UTF8') cur = conn.cursor() #the above code executed without exception #and [server closed the connection unexpectedly] thrown when any cur.execute() called I think it's strange because the server reset connection after connected,and the connect function should throw a exception.
You sure you are not doing anything contrary to the instructions here: