Ramon
Ramon

Reputation: 73

In Python how to capture Redshift's response on COPY command?

I used SQLAlchemy + PyGreSQL to send COPY command to Redshift to import CSV/JSON files into database tables. After COPY command was executed, a message line was sent back on screen:

INFO:  Load into table 'o_item_details' completed, 20443 record(s) loaded successfully.

How do I capture this message so I can push it into logging function which writes logs to files? Here's my code:

engine = create_engine("postgresql+pygresql://{0}:{1}@{2}:{3}/{4}".format(rs_user, rs_pwd, rs_host, rs_port, rs_dbname))
conn = engine.connect()
re = conn.execute(sql_cmd) 

sql_cmd is the COPY statement (copy {rs_tbl_name} from s3://{s3_bucket}/{s3_file} ... )

I played around the ResultProxy object returned by execute function but didn't find a way to retrieve the message.

Any help is much appreciated!

Upvotes: 1

Views: 1502

Answers (1)

Adam Owczarczyk
Adam Owczarczyk

Reputation: 2862

Since stackoverflow does not allow short answers, I had to add this sentence you are reading:)

print(conn.notices)

Upvotes: 3

Related Questions