从 psycopg2 中的模式获取数据

Getting data from schema in psycopg2

我尝试在我的 Python 应用程序中连接到 Postgres 数据库。 我使用 psycopg2 库。

如果我像这样执行 select 语句:

cursor.execute("""select schema_name from information_schema.schemata;""")
rows = cursor.fetchall()
print(rows)

我得到:

[('DB_FZ',), ('DB_FZ_TEMP',), ...]

但是如果我执行语句

cursor.execute("""select * from DB_FZ.d_results;""")
rows = cursor.fetchall()
print(rows)

我收到错误

psycopg2.ProgrammingError: schema "db_fz" does not exist
LINE 1: select * from DB_FZ.d_results;

有什么问题吗?

模式名称是大写的,你应该把它放在双引号中:

cursor.execute("""select * from "DB_FZ".d_results;""")

另见 Are PostgreSQL column names case-sensitive?