Trying to create a Redshift table using Python and psycopg2 but the table does not get created with no errors reported

江枫思渺然 提交于 2020-03-05 04:14:06

问题


My codes return no error but I don't see a table in Redshift...if I put "if table exist" and try to create a table I know exists it does nothing and returns no error. Take that out and it will return duplicatetable error which is odd.

import boto3
import psycopg2
import sys

#Assign global variables data needed to make connection to Redshift
DB_NAME = '<database>'
CLUSTER_IDENTIFIER = '<clusterName>'
DB_USER = '<user>'
ENDPOINT = '<clustername>.<randomkey>.us-east-1.redshift.amazonaws.com'
REGION = 'us-east-1'

sql = "CREATE TABLE if not exists " + "<schema>.<tablename> " + \
      "( vendorid varchar(4), pickup_datetime TIMESTAMP, " + \
      "dropoff_datetime TIMESTAMP, store_and_fwd_flag varchar(1), " + \
      "ratecode int, pickup_longitude float(4), pickup_latitude float(4)," + \
      "dropoff_logitude float(4), dropoff_latitude float(4), " + \
      "passenger_count int, trip_distance float(40), fare_amount float(4), " + \
      "extra float(4), mta_tax float(4), tip_amount float(4), " + \
      "tolls_amount float(4), ehail_fee float(4), improvement_surcharge float(4), " + \
      "total_amount float(4), payment_type varchar(4), trip_type varchar(4))  " + \
      "DISTSTYLE EVEN SORTKEY (passenger_count, pickup_datetime);"

try:
    #make redshift connection
    client = boto3.client('redshift', region_name='us-east-1')

    #get temporary username and password
    cluster_creds = client.get_cluster_credentials(DbUser=DB_USER, DbName=DB_NAME, ClusterIdentifier=CLUSTER_IDENTIFIER, AutoCreate=False)
    temp_user = cluster_creds['DbUser']
    temp_pswd = cluster_creds['DbPassword']

    #create connection string to database
    conn = psycopg2.connect(f"host='{ENDPOINT}' port='5439' user={temp_user} password={temp_pswd} dbname='{DB_NAME}'")

    #Attempt to create table
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit
    cursor.close()
    conn.close()

    #report any errors
except Exception as ex:
    print("Exception name : " + ex.__class__.__name__)
    print(str(ex))
    print("Failed to open connection to Redshift database")
    sys.exit(1)


回答1:


My codes return no error but I don't see a table in Redshift...if I put "if table exist" and try to create a table I know exists it does nothing and returns no error. Take that out and it will return duplicatetable error which is odd.

There is no problem with code or redshift. Whatever is happening is exactly expected.

if I put "if table exist" and try to create a table I know exists it does nothing and returns no error

This is expected as per Redshift documentation. Nothing wrong with it. Below goes documentation excerpt for if not exist.

IF NOT EXISTS

Clause that indicates that if the specified table already exists, the command should make no changes and return a message that the table exists, rather than terminating with an error. Note that the existing table might be nothing like the one that would have been created; only the table name is used for comparison.

This clause is useful when scripting, so the script doesn’t fail if CREATE TABLE tries to create a table that already exists.

Take that out and it will return duplicatetable error which is odd.

Its expected, table already exist, hence duplicate table error.

I don't see a table in Redshift

This should be an issue that either the user you are using to view table is not having rights to view that table or looking into some-other database by mistake. To prove my point that table exist, try inserting some records in table using your program and try selecting those records, if it happens, then its proved that table exists and having the data. Other user that you might be using to see table may not have rights to view it.

I hope it helps.



来源:https://stackoverflow.com/questions/57929991/trying-to-create-a-redshift-table-using-python-and-psycopg2-but-the-table-does-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!