繁体   English   中英

psycopg2 - Postgresql 数据库中的“关系不存在”

[英]psycopg2 - 'Relation does not exist' in Postgresql database

我试图弄清楚为什么我不能使用 psycopg2 访问 PostgreSQL 数据库中的特定表。 我正在运行 PostgreSQL 11.5

如果这样做,我可以连接到有问题的数据库并读取其中的所有表:

import psycopg2

try:
connection = psycopg2.connect(user = "postgres",                #psycopg2.connect() creates connection to PostgreSQL database instance
                              password = "battlebot",
                              host = "127.0.0.1",
                              port = "5432",
                              database = "BRE_2019")

cursor = connection.cursor()                                #creates a cursor object which allows us to execute PostgreSQL commands through python source

#Print PostgreSQL version
cursor.execute("""SELECT table_name FROM information_schema.tables
   WHERE table_schema = 'public'""")

for table in cursor.fetchall():
    print(table)

结果如下所示:

('geography_columns',)
('geometry_columns',)
('spatial_ref_sys',)
('raster_columns',)
('raster_overviews',)
('nc_avery_parcels_poly',)
('Zone5e',)
('AllResidential2019',)
#....etc....

我感兴趣的表是最后一张,“AllResidential2019”

所以我尝试连接到它并通过执行以下操作打印内容:

try:
    connection = psycopg2.connect(user = "postgres",                
    #psycopg2.connect() creates connection to PostgreSQL database instance
                              password = "battlebot",
                              host = "127.0.0.1",
                              port = "5432",
                              database = "BRE_2019")

    cursor = connection.cursor()                                #creates a cursor object which allows us to execute PostgreSQL commands through python source

    cursor.execute("SELECT * FROM AllResidential2019;")           #Executes a database operation or query. Execute method takes SQL query as a parameter. Returns list of result
    record = cursor.fetchall()

    print(record)


except (Exception, psycopg2.Error) as error:
    print("Error while connecting to PostgreSQL: ", error)

我收到以下错误:

Error while connecting to PostgreSQL:  relation "allresidential2019" does not exist
LINE 1: SELECT * FROM AllResidential2019;

但是,当我尝试连接到我拥有的另一个数据库中的另一个表时,我可以成功连接并获得结果(这有效!结果是该表中的数据):

try:
    connection = psycopg2.connect(user = "postgres",                #psycopg2.connect() creates connection to PostgreSQL database instance
                              password = "battlebot",
                              host = "127.0.0.1",
                              port = "5432",
                              database = "ClimbingWeatherApp") .  #different database name

    cursor = connection.cursor()                                


    cursor.execute("SELECT * FROM climbing_area_info ;")          
    record = cursor.fetchall()

    print(record)


except (Exception, psycopg2.Error) as error:
    print("Error while connecting to PostgreSQL: ", error)

我不知道为什么我可以使用完全相同的代码从一个表而不是另一个表中检索信息(名称是更改除外)。 而且我也不知道如何解决这个问题。 任何人都可以提供建议吗?

您的表名区分大小写,您必须用双引号将其关闭:

SELECT * FROM "AllResidential2019";

在 Python 程序中,它可能如下所示:

cursor.execute('SELECT * FROM "AllResidential2019"')

或者您可以使用专门的模块SQL 字符串组合:

from psycopg2 import sql

# ...

cursor.execute(sql.SQL("SELECT * FROM {}").format(sql.Identifier('AllResidential2019')))

请注意,区分大小写的 Postgres 标识符(即表、列、视图、函数等的名称)不必要地使简单的事情复杂化。 我建议你不要使用它们。

很可能,您的问题的原因是 Postgres 的引用规则遵守关于双引号标识符的 ANSI SQL 标准。 在创建表时,您可能引用了该表:

CREATE TABLE "AllResidential2019" (
   ...
)

由于至少一个大写字母区分大小写,这要求您在引用表格时始终引用表格。 请记住:单引号和双引号在 SQL 中具有不同的含义,而不是在 Python 中大部分可以互换。

SELECT * FROM "AllResidential2019"
DELETE FROM "AllResidential2019" ...
ALTER TABLE "AllResidential2019" ...

如果您的表、列或其他标识符不包含特殊字符、空格或保留字,通常建议始终使用小写字母或不使用引号:

CREATE TABLE "allresidential2019" (
   ...
)

CREATE TABLE AllResidential2019 (
   ...
)

这样做,任何大写字母组合都可以

SELECT * FROM ALLRESIDENTIAL2019
SELECT * FROM aLlrEsIdEnTiAl2019
SELECT * FROM "allresidential2019"

请参阅有关该主题的进一步阅读:

我在 Ubuntu 中遇到了同样的错误。 但就我而言,我不小心将表添加到了错误的数据库中,而该数据库又归根 postgres 用户所有,而不是我为烧瓶应用程序创建的新 postgres 用户所有。

我正在使用 SQL 文件来创建和填充表。 这是我曾经能够使用.sql文件创建这些表的命令。 这允许您指定表的所有者以及应在其中创建它们的数据库:

sudo -u postgres psql -U my_user -d my_database -f file.sql -h localhost

然后将提示您输入my_users的密码。

sudo -u postgres仅当您以 root 用户身份从终端运行时才需要。 它基本上以postgres用户身份运行psql ...命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM