You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda连接AWS Redshift报错:relation 'vault_control'不存在求助

解决Redshift Data API查询提示“relation does not exist”问题

问题背景

AWS Redshift集群中存在vault_control表,通过DBeaver执行select distinct(solution_name) from vault_control;可正常返回结果,但使用AWS Lambda的boto3 redshift-data客户端执行相同查询时,报错ERROR: relation "vault_control" does not exist。

可能原因及解决方案

1. 查询未指定表所在的Schema

DBeaver通常会自动配置默认Schema(比如连接时指定或用户预设),但Redshift Data API默认使用public Schema。如果vault_control表不在public Schema下,就会出现找不到表的错误。

解决步骤:

  • 先在DBeaver中执行以下SQL,确认表所属的Schema:
select schemaname, tablename from pg_tables where tablename = 'vault_control';
  • 方案一:修改查询语句,显式指定Schema:
select distinct(solution_name) from [你的Schema名称].vault_control;
  • 方案二:在Lambda的execute_statement调用中添加Schema参数:
execution_id = client.execute_statement(
    ClusterIdentifier='id',
    Database='cloudbi',
    DbUser='user',
    Sql=query_string,
    Schema='[你的Schema名称]'  # 替换为实际Schema名称
)['Id']

2. 核对连接参数一致性

检查Lambda代码中的ClusterIdentifier、Database、DbUser是否与DBeaver使用的完全一致,避免因连接到错误的集群/数据库导致表不存在。

3. 验证数据库用户权限

确保使用的DbUser拥有目标Schema和表的访问权限,可执行以下SQL授权:

GRANT USAGE ON SCHEMA [你的Schema名称] TO user;
GRANT SELECT ON TABLE [你的Schema名称].vault_control TO user;

修正后的Lambda代码示例

import json
import boto3
import time

# 替换为带Schema的查询语句
query_string = "select distinct(solution_name) from your_schema.vault_control;"

def lambda_handler(event, context):
    client = boto3.client('redshift-data')
    execution_id = client.execute_statement(
        ClusterIdentifier='id',
        Database='cloudbi',
        DbUser='user',
        Sql=query_string
        # 也可以用Schema参数指定:Schema='your_schema'
    )['Id']
    print(f'Execution started with ID {execution_id}')

    status = client.describe_statement(Id=execution_id)['Status']
    while status not in ['FINISHED','ABORTED','FAILED']:
        time.sleep(10)
        status = client.describe_statement(Id=execution_id)['Status']
    print(f'Execution {execution_id} finished with status {status}')

    if status == 'FINISHED':
        result = client.get_statement_result(Id=execution_id)
        columns = [c['label'] for c in result['ColumnMetadata']]
        records = result['Records']
        print(f'SUCCESS. Found {len(records)} records')
    else:
        error_msg = client.describe_statement(Id=execution_id)["Error"]
        print(f'Failed with Error: {error_msg}')

内容的提问来源于stack exchange,提问作者user3476582

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:48:23