[英]Psycopg2 relation db does not exist
我最近開始使用 Macbook,因為我的筆記本電腦在工作時發生了變化,之后我開始遇到一些用於將數據幀上傳到 postgresql 數據庫的代碼的問題。
import psycopg2
from io import StringIO
def create_connection(user,password):
return psycopg2.connect(
host='HOST',
database='DBNAME',
user=user,
password=password)
conn = create_connection(user,password)
table = "data_analytics.tbl_summary_wingmans_rt"
buffer = StringIO()
df.to_csv(buffer, header=False, index=False)
buffer.seek(0)
cursor = conn.cursor()
cursor.copy_from(buffer, table, sep=",", null="")
conn.commit()
cursor.close()
如您所見,代碼非常簡單,即使在更換設備之前,它在 Windows 上也沒有出現重大問題。 但是一旦我在 mac 上運行相同的代碼,它就會拋出以下錯誤:
Error: relation "data_analytics.tbl_summary_wingmans_rt" does not exist
在幾篇文章中,我看到可能使用了雙引號,但我已經使用了以下內容,但仍然沒有得到肯定的結果。
"data_analytics."tbl_summary_wingmans_rt""
""data_analytics"."tbl_summary_wingmans_rt""
'data_analytics."tbl_summary_wingmans_rt"'
copy_from
的行為在 psycopg2 2.9 中更改為正確引用表名,這意味着您不能再以這種方式提供模式限定的表名; 您必須改用copy_expert
。
現在,當您發送"data_analytics.tbl_summary_wingmans_rt"
它是一個字符串並且無法解析時,您必須在將其發送到 Postgres 解析器之前將模式和表分開
使用'"data_analytics"."tbl_summary_wingmans_rt"'
這會將輸出解析為 "schema"."table" 並且 PostgreSQL 將能夠解析
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.