I tried to write a function to drop database :
def deleteDb(self, dbName: str):
conn = psycopg2.connect(dbname="postgres", user="postgres")
conn.autocommit = True
curs = conn.cursor()
curs.execute("DROP DATABASE {};".format(dbName))
curs.close()
conn.close()
When I try to test it with an existing db :
def test_deleteDb(self):
self.deleteDb("dbTest")
I get this error : "psycopg2.errors.InvalidCatalogName: database "dbtest" does not exist"
I tried to play with the isolation level, to drop all the connections to the database and to connect directly to the database but it did not work
CodePudding user response:
Remember to put quotes around identifiers that contain upper-case letters:
curs.execute('DROP DATABASE "{}";'.format(dbName))
Note that string substitution into SQL-statements is generally a bad idea beause it is vulnerable to SQL-injection.