PostgreSQL: Kept getting ERROR: Connection to the server has been lost

Hi team, when administering PostgreSQL, especially when running bulk insert or large batch of maintenance script via pgAdmin4, I receive ERROR: Connection to the server has been lost., and none of the scripts are executed properly

Upon more trial and error, if the batch contains more than 40K commands, it will time out…