mfenniak / pg8000

A Pure-Python PostgreSQL Driver
http://pythonhosted.org/pg8000/
Other
222 stars 55 forks source link

no exception raised when creating a role that already exists #168

Closed JayArbo closed 5 years ago

JayArbo commented 6 years ago

postgres raises an error if you try to create a role that already exists.. pg8000 doesn't report this error. sample below...

import pg8000 from sys import exc_info

connection = pg8000.connect("postgres", host='localhost', database="postgres") connection.autocommit = True cursor = connection.cursor() cmd = "CREATE ROLE %s PASSWORD '%s' LOGIN" % ("pythonuser2", "password") try:

doesn't raise exception when user already exists

#expected: ERROR:  role "pythonuser2" already exists
cursor.execute(cmd)

except: e = exc_info()[1] #never executes connection.autocommit = False

connection.close

tlocke commented 5 years ago

Hi @JayArbo, I think that what's happening here is that your except block is swallowing the exception. I tried running:

import pg8000

connection = pg8000.connect("postgres", host='localhost', database="postgres")
connection.autocommit = True
cursor = connection.cursor()
cursor.execute("CREATE ROLE pythonuser2 PASSWORD 'password' LOGIN")
connection.close()

And this does indeed raise an exception the second time it's run.