首页 > 解决方案 > “无法复制到不存在的表”错误,但表存在于 Amazon Redshift 中

问题描述

我在 Redshift 中设置了一个表,现在想用来自不同区域的 s3 存储桶的数据填充它。我正在使用 COPY 命令,但出现错误:

    "psycopg2.errors.InternalError_: Cannot COPY into nonexistent table customcontent_table"

我无法弄清楚如何解决它,因为该表显然已经存在。我的语法有错误吗?我的代码:

    sql = "copy customcontent_table from 'test/2021/03/29/20/20/CustomContent.snappy.parquet' credentials 'aws_access_key_id=AA;aws_secret_access_key=zz' format parquet region 'us-west-2';"
    cur = con.cursor()
    cur.execute("begin;")
    cur.execute(sql)
    cur.execute("commit;")
    con.close()    

标签: amazon-web-servicesamazon-redshiftpsycopg2

解决方案


因此,您对 S3 对象的引用看起来不正确。应该类似于(根据 AWS 文档):

copy listing
from 's3://mybucket/data/listings_pipe.txt'
access_key_id '<access-key-id>'
secret_access_key '<secret-access-key'
...;

您似乎只有对象键,但没有 s3:// 前缀和存储桶名称。我不认为这是导致此错误的原因,但您需要修复它。

我最初的想法是为什么您会收到此错误消息,因为此会话未找到该表。Redshift 会话有一个“搜索路径”的概念,它告诉当前会话在哪里查找表(哪些模式)。如果是这种情况,那么最简单的解决方案(或者至少是最容易解释的)就是将表的模式添加到 COPY 命令中:

copy schema_name.customcontent_table from ...

这将告诉 Redshift 在哪里可以找到该表。如果您想设置搜索路径,可以在此处阅读 - https://docs.aws.amazon.com/redshift/latest/dg/r_search_path.html

如果这不是问题,那么我们需要更深入地挖掘。


推荐阅读