AWS Redshift: Read & Write to database in python

Shivika K Bisen
Bright AI
Published in
1 min readOct 22, 2022

AWS Redshift is a Data Warehouse used as the efficient source of many Machine learning models deployed in the cloud and the data from Redshift can be easily read in python script in code editors or in jupyter notebook/ colab

Read data from AWS Redshift

Step 1: Import sqlalchemy and pandas library

Step 2: Create the redshift_engine with the below syntax, and add the AWS credentials: host, username, password, port, and dbname

Step 3: Ensure that the table is already existing in AWS Redshift. Read that table as df_read

Write data to AWS Redshift

This approach works when the table is already created in the AWS Redshift, with defined column names & data format

Step 1: Import sqlalchemyand pandasif haven’t done it earlier

Step 2: Create the redshift_enginewith the below syntax, and add the AWS credentials: host, username, password, port, and dbname

Step3: Write data frame df_write to Redshift

  • Define the data type for each column as existing in the Redshift table
  • To replace the complete data in the redshift table, set the parameter if_exists = 'replace' . If you simply wish to append new data to an existing one if_exists = 'append'

--

--

Shivika K Bisen
Bright AI

Gen AI/ML, Data Scientist | University of Michigan Alum | Generative AI, Recommendation & Search & NLP, Predictive models. https://sbisen.github.io/