I want to use the sql submodule of psycopg2 to write clean dynamic SQL:
from psycopg2 import sql
...
cursor.execute(sql.SQL("SELECT * FROM {}").format(sql.Identifier('myschema.mytable'))
This creates the following query:
SELECT * FROM "myschema.mytable"
Here I get an Relation "myschema.mytable" not found.
exception.
How do I handle the schema name properly? The following statements would work, but how do I create them with psycopg2?
SELECT * FROM myschema.mytable
SELECT * FROM myschema."mytable"
SELECT * FROM "myschema"."mytable"
edit: clarified schema prefix
SELECT * FROM mytable;
works fine whileSELECT * FROM "mytable";
gives an error. I have to add that mytable contains an schema prefix, which may cause the problem. – Portable