I'm having issues with executing multiple queries on my psql db using psycopg2. Example:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import psycopg2
from psycopg2.extras import RealDictCursor
def CreateUser(user, mySchema):
conn = psycopg2.connect("dbname='postgres' user='root' password='somePassword' host='localhost'")
cur = conn.cursor()
cur.execute("""create user %s""" % (user))
conn.commit()
cur.close()
conn.close()
CreateSchema(user, mySchema)
def CreateSchema(user, mySchema):
conn = psycopg2.connect("dbname='postgres' user='root' password='somePassword' host='localhost'")
cur = conn.cursor()
cur.execute("""create schema %s authorization %s """ % (user,mySchema))
conn.commit()
cur.close()
conn.close()
def FetchUserInput():
userInput = raw_input("UserName")
mySchema = raw_input("SchemaName")
CreateUser(userInput, mySchema)
FetchUserInput()
In this case, the second query fails with an error that the user created previously does not exist! If I only execute the CreateUser function, it works fine. If I execute it manually within psql, it works fine.
As If the first commit isnt executed on the database when I open up a second connection within the CreateSchema function, which makes no sense.
What am I doing wrong?