Categories
Connecting to Data Stores Guides and Code Samples

Connecting to Redshift

Connecting to Redshift

Here’s a quick example of how to connect to Redshift using JetML workflows. Note that your cluster must be publicly accessible.  

Workflow Setup
  1. Add the redshift_connector package to enable Python to connect to Redshift.
  2. Add your Redshift credentials as environment variables to keep them separate than your code.
  3. Run the workflow.
  4. Copy over the example code below into a notebook on the running workflow.
Example Notebook
import redshift_connector
import os
conn = redshift_connector.connect(
     host=os.environ['rs_host'],
     database=os.environ['rs_database'],
     user=os.environ['rs_user'],
     password=os.environ['rs_password']
  )
cursor: redshift_connector.Cursor = conn.cursor()
cursor.execute("select * from sales limit 10")

result: tuple = cursor.fetchall()
print(result)
([65082, 73790, 20429, 451, 1150, 1827, 4, Decimal('472.00'), Decimal('70.80'), datetime.datetime(2008, 1, 1, 6, 6, 57)], [97197, 110942, 43282, 515, 7003, 1827, 2, Decimal('708.00'), Decimal('106.20'), datetime.datetime(2008, 1, 1, 2, 30, 52)], [110328, 126347, 6955, 394, 6213, 1827, 1, Decimal('347.00'), Decimal('52.05'), datetime.datetime(2008, 1, 1, 1, 0, 19)], [165890, 222879, 2186, 664, 6870, 1827, 2, Decimal('4192.00'), Decimal('628.80'), datetime.datetime(2008, 1, 1, 12, 59, 34)], [49137, 55510, 30285, 91, 674, 1828, 2, Decimal('108.00'), Decimal('16.20'), datetime.datetime(2008, 1, 2, 5, 13, 52)], [97198, 110942, 43282, 1532, 7003, 1828, 1, Decimal('354.00'), Decimal('53.10'), datetime.datetime(2008, 1, 2, 2, 31, 12)], [98431, 112471, 33657, 894, 2618, 1828, 1, Decimal('269.00'), Decimal('40.35'), datetime.datetime(2008, 1, 2, 4, 4, 32)], [160330, 212426, 37430, 447, 6884, 1828, 4, Decimal('8672.00'), Decimal('1300.80'), datetime.datetime(2008, 1, 2, 3, 20, 32)], [7794, 8410, 16243, 2378, 6157, 1829, 3, Decimal('603.00'), Decimal('90.45'), datetime.datetime(2008, 1, 3, 1, 18, 41)], [64837, 73489, 21131, 2375, 4488, 1829, 2, Decimal('156.00'), Decimal('23.40'), datetime.datetime(2008, 1, 3, 5, 58, 2)])