I want to create spatial indexes in all the tables that are included in a schema in PostGIS. The layers remain the same but the schema is everytime different. So I wanted to pass a variable var = schemaname + "l_abluft(geom)" to the SQL query (CREATE INDEX ON test.l_abluft(geom);)
#--Create a Connection to PostGIS
#---------------------------------------------------------------------------
connection = psycopg2.connect (dbname = "xxxxx",
user = "xxxxxx",
password = "xxxxx",
host = "xxxxxx",
)
cursor = connection.cursor()
#--Create spatial Indexes
#---------------------------------------------------------------------------
cursor.execute("CREATE INDEX ON test.l_abluft(geom);")
cursor.execute("CREATE INDEX ON test.l_zuluft(geom);")
cursor.execute("CREATE INDEX ON test.rl_isolierung(geom);")
cursor.execute("CREATE INDEX ON test.treppenhaus(geom);")
cursor.execute("CREATE INDEX ON test.waende_saeule_usw(geom);")
connection.commit()
print("Query successful")
I tried the following but I got the error: psycopg2.errors.SyntaxError: syntax error at end of input LINE 1: CREATE INDEX ON "test.l_abluft(geom)"
schemaname = "test"
var = schemaname + ".l_abluft(geom)"
#--Create spatial Indexes
#---------------------------------------------------------------------------
cursor.execute(
sql.SQL("CREATE INDEX ON {}").format(sql.Identifier(var)))
connection.commit()
print("Query successful")
str.format()
orstr.replace()
to add the parts that do.