【问题标题】:UndefinedTable: relation table does not exist when using `copy_from` methodUndefinedTable:使用 `copy_from` 方法时关系表不存在
【发布时间】:2022-01-11 18:37:50
【问题描述】:

我正在尝试编写一个脚本,在我的 Postgres 数据库中创建一个新表,并使用 psycopg2 库将现有的 csv 文件复制到该表中。需要明确的是,我严格尝试使用psycopg2 来执行此操作,而不是使用psql.exe 或ogr2ogr. 等其他方法到目前为止,我可以连接、创建表并对现有表进行测试查询。 *注意:这个数据库有多个模式。我创建了一个名为“测试”的表格来放置我的表格。

import os
import sys
import csv
import psycopg2
from os import path
from datetime import datetime

csv_file = r"C:\Projects\csv_report_to_sql\test\csv_report.csv"

conn = psycopg2.connect(dbname='mydb', user='postgres', password='mypassword', host='www.mydbserver.com', port='5432', sslmode='require')
cur = conn.cursor()
cur.execute("""
    CREATE TABLE test.new_table
    (
        region TEXT,
        state TEXT,
        tier TEXT,
        v_detailed DOUBLE PRECISION,
        v_approx DOUBLE PRECISION,
        v_unmapped DOUBLE PRECISION,
        v_total DOUBLE PRECISION,
        a_detailed DOUBLE PRECISION,
        a_approx DOUBLE PRECISION,
        a_unmapped DOUBLE PRECISION,
        a_total DOUBLE PRECISION
    )
""")
conn.commit()

我现在可以看到 test 架构下的表,但要在脚本中测试它,我可以运行:

cur.execute('SELECT * FROM test.new_table')
one = cur.fetchone()
print(one)

它返回“None”,将其更正为空表。现在,我想将我的 csv 文件数据复制到这个新表中。我是psycopg2 的新手,但从这个tutorial 开始,我假设我必须建立一个新连接,因为旧连接关闭。

conn = psycopg2.connect(dbname='mydb', user='postgres', password='mypassword', host='www.mydbserver.com', port='5432', sslmode='require')
cur = conn.cursor()
with open(csv_file, 'r') as f:
    next(f) # Skip the header row.
    cur.copy_from(f, 'test.new_table', sep=',')

conn.commit()

当我运行上面的,我得到以下错误:

UndefinedTable:关系“new_table”不存在

我可以清楚地看到该表存在,那我这里做错了什么?

【问题讨论】:

  • 您应该可以重复使用您的conn。如果它没有在with psycopg2.connect(...) as conn: 中打开,它不会自动关闭。这样您就可以确保在代码的两个部分中都与同一个数据库进行交互。重复使用连接也是一个好习惯,因为不断打开和关闭的开销是不必要的。
  • 是的,你是对的。忽略那部分。我只是假设它在 commit() 之后关闭。
  • 我无法复制。尝试在一个连接下完成所有操作,看看会发生什么?我可以看到这种情况发生的唯一方法是使用两个不同的数据库会话,其中一个创建表没有提交,所以另一个看不到表。

标签: python postgresql copy psycopg2


【解决方案1】:

好的,这是一个奇怪的修复,但我认为它可能与在 Jupyter Notebook 的单独单元格中运行它有关。基本上,我只是在一个单元格中将所有内容组合在一起(按照建议删除了第二个连接)并且它运行正常。假设在独立脚本中运行它没有问题。

import os
import sys
import psycopg2
import csv

csv_file = r"C:\Projects\csv_report_to_sql\test\csv_report.csv"

conn = psycopg2.connect(dbname='mydb', user='postgres', password='mypassword', 
host='www.mydbserver.com', port='5432', sslmode='require')
cur = conn.cursor()
cur.execute("""
    CREATE TABLE test.new_table
    (
        region TEXT,
        state TEXT,
        tier TEXT,
        v_detailed DOUBLE PRECISION,
        v_approx DOUBLE PRECISION,
        v_unmapped DOUBLE PRECISION,
        v_total DOUBLE PRECISION,
        a_detailed DOUBLE PRECISION,
        a_approx DOUBLE PRECISION,
        a_unmapped DOUBLE PRECISION,
        a_total DOUBLE PRECISION
    )
""")
conn.commit()
with open(csv_file, 'r') as f:
    next(f) # Skip the header row.
    cur.copy_from(f, 'test.new_table', sep=',')

conn.commit()
conn.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 2016-03-18
    • 2015-07-29
    • 2020-08-08
    • 1970-01-01
    • 2013-10-06
    • 2020-05-08
    相关资源
    最近更新 更多