Python Custom With Statement Postgresql