Postgresql While Loop Exit Python Interpreter Vs Terminal