Postgresql While Loop Exit Python Command Line