【问题标题】:Psycopg2 relation db does not existPsycopg2 关系数据库不存在
【发布时间】:2021-09-16 17:45:37
【问题描述】:

我最近开始使用 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"'

【问题讨论】:

标签: python sql pandas postgresql psycopg2


【解决方案1】:

copy_from 的行为在 psycopg2 2.9 中更改为正确引用表名,这意味着您不能再以这种方式提供模式限定的表名;你必须改用copy_expert

【讨论】:

  • 这很疯狂,但正确! ?
【解决方案2】:

您现在必须先将模式和表分开,然后再将其发送到 Postgres 解析器, 当您发送 "data_analytics.tbl_summary_wingmans_rt" 时,它是一个字符串并且无法解析

使用'"data_analytics"."tbl_summary_wingmans_rt"'这会将输出解析为“schema”.“table”并且PostgreSQL将能够解析

【讨论】:

  • 我试了一下,一直收到同样的错误错误:关系““data_analytics”。“tbl_summary_wingmans_rt”不存在
  • @Kev :我遇到了同样的问题。 2.9 的 psycopg2 文档说,如果您必须指定模式,则使用 copy_expert( ..) 。它并没有说引用架构会起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
相关资源
最近更新 更多