this code will prevent the app from responding to SIGTERM, because you're catching and suppressing _all_ exceptions.  You either need to catch just PG Exceptions or at the end of the block, re-raise anything that is not a PG exception.