使用psycopg2扩展名ISOLATION_LEVEL_AUTOCOMMIT:
发出命令且不需要commit()或rollback()时,不会启动任何事务。
import psycopg2from psycopg2 import sqlfrom psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT # <-- ADD THIS LINEcon = psycopg2.connect(dbname='postgres', user=self.user_name, host='', password=self.password)con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # <-- ADD THIS LINEcur = con.cursor()# Use the psycopg2.sql module instead of string concatenation # in order to avoid sql injection attacs.cur.execute(sql.SQL("CREATE DATAbase {}").format( sql.Identifier(self.db_name)) )


