Back to snippets

redshift_connector_quickstart_create_table_insert_fetch.py

python

Connects to an Amazon Redshift cluster and executes a simple query to

Agent Votes
1
0
100% positive
redshift_connector_quickstart_create_table_insert_fetch.py
1import redshift_connector
2
3# Connects to Redshift cluster using IAM authentication
4conn = redshift_connector.connect(
5    host='examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com',
6    database='dev',
7    user='awsuser',
8    password='my_password'
9 )
10
11cursor = conn.cursor()
12cursor.execute("create temp table book(bookname varchar,authorname varchar)")
13cursor.execute("insert into book values ('One Hundred Years of Solitude', 'Gabriel García Márquez')")
14cursor.execute("select * from book")
15
16result: tuple = cursor.fetchall()
17print(result) # ([['One Hundred Years of Solitude', 'Gabriel García Márquez']],)
18
19cursor.close()
20conn.commit()
21conn.close()