【问题标题】:psycopg2 change schema does not workpsycopg2 更改架构不起作用
【发布时间】:2017-12-09 10:07:48
【问题描述】:

我正在编写一个简单的 python prog 来连接和显示来自 AWS RDS 上的 Postgres 表的结果。我在公共架构中有表 mytest。

connection = psycopg2.connect(dbname='some_test',
                    user='user1',
                    host='localhost',
                    password='userpwd',
                    port=postgres_port)
cursor = connection.cursor()
cursor.execute("SET SEARCH_PATH TO public;")
cursor.execute("SELECT *  FROM mytest;")

但这会引发错误

psycopg2.ProgrammingError: 关系“mytest”不存在

第 1 行:从 mytest 中选择 *;

连接成功,我可以查询其他基表,如

SELECT table_name FROM information_schema.tables

只是我无法更改为任何其他架构。我用谷歌搜索并尝试了各种 SET SERACH_PATH 并提交它并重新创建光标等,但没有用。我无法查询任何其他架构。

【问题讨论】:

  • 您确定您的表mytest 是在public 架构中创建的吗? SEARCH_PATH 参数的默认值是 '$user', public 并且如果 1)您有与连接用户同名的架构,并且 2)您在未指定架构名称的情况下创建了表,那么该表可能是在架构架构中创建的user1(来自您的示例)而不是 public。查找您的餐桌:SELECT table_schema, table_name FROM information_schema.tables where table_name ilike 'mytests';More details
  • 感谢您的回复。我执行了 SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE 'mytest';结果是 - table_schema = public & table_name = mytest 所以我可以确认它是在 public 下创建的。默认架构我看到我的连接的“pg_catalog”而不是公开的。我可以访问所有 pg_catalog 表。
  • 一切正常,字符大小写,你的表完全命名为mytests,而不是MyTests
  • 是的!全部都是小写 - SELECT * FROM information_schema.tables WHERE table_name LIKE 'mytest';表名“mytest”,它只有 2 列。一个是 int4 length-10,另一个是文本,如果有帮助的话。
  • 它对我有用 github.com/JamesSjaalman/gigabert/blob/master/hubertc.c (line#133) 如果它对你不起作用,它可能是 amazon-rds -glitch(或 psycopg 中的某些东西)

标签: postgresql psycopg2 amazon-rds postgresql-9.4 rds


【解决方案1】:
ALTER USER username SET search_path = schema1,schema2;

设置后查询工作正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多