better logging and try catch for psycopg2 and socket error

Signed-off-by: Marco Milanesio <milanesio.marco@gmail.com>