def create_db():
try:<EOL><INDENT>return psycopg2.connect(**db_state['<STR_LIT>'])<EOL><DEDENT>except psycopg2.OperationalError as exc:<EOL><INDENT>nosuch_db = '<STR_LIT>' % db_state['<STR_LIT>']['<STR_LIT>']<EOL>if nosuch_db in str(exc):<EOL><INDENT>try:<EOL><INDENT>master = psycopg2.connect(database='<STR_LIT>')<EOL>master.rollback()<EOL>master.autocommit = True<EOL>cursor = master.cursor()<EOL>cursor.execute('<STR_LIT>' % db_state['<STR_LIT>']['<STR_LIT>'])<EOL>cursor.close()<EOL>master.close()<EOL><DEDENT>except psycopg2.Error as exc:<EOL><INDENT>message = ('<STR_LIT>'<EOL>+ db_state['<STR_LIT>']['<STR_LIT>']<EOL>+ '<STR_LIT>' % exc)<EOL>raise RuntimeError(message)<EOL><DEDENT>else:<EOL><INDENT>conn = psycopg2.connect(**db_state['<STR_LIT>'])<EOL>db_state['<STR_LIT>'] = True<EOL>return conn<EOL><DEDENT><DEDENT><DEDENT>
connect to test db