How to check if a aws redshift table is present or not

Question:

I have an use case where I need to do the followings:

1.Check if redshift_db.redhsift_tbl is present or not? If table is present then perform operation A else perform operation B.

I was checking in boto3 documentation to get an api which will tell whether or not the table is present but no luck. What is the best way to check whether a redshift table is present or not?

Asked By: Dcook

||

Answers:

There are 2 ways you can go. If you are using an external orchestration tool then this tool can query the catalog tables (pg_table_def or stv_tbl_perm) and issue the appropriate next commands based on the result. Or if you need Redshift to do this then you will need create a stored procedure to take the correct action based on examining the catalog tables.

Answered By: Bill Weiner

You can use the boto3 RedshiftDataAPIService list_tables function (boto3 v1.25.2). https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift-data.html#RedshiftDataAPIService.Client.list_tables

eg.

session = boto3.Session(
        botocore_session=botocore.session.get_session()
        region_name=region)
redshift_client = session.client("redshift-data")
tables = redshift_client.list_tables(
            Database={database}
            SecretArn={secretarn}
            ClusterIdentifier={clusteridentifier}
            SchemaPattern={schemapattern}, #optional
            TablePattern={patternpattern}) #optional but recommended

From there, you can check the response to see if the table you are looking for is present. If no schema pattern and/or table pattern is selected there are A LOT of results so I recommend using tablepattern=tablename to limit them.

Answered By: Sophs