簡體   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