DEV Community

Shoeb Ahmed
Shoeb Ahmed

Posted on

AWS Redshift connects with Python Part 2 — Python code to connect AWS Redshift

AWS Redshift connects with Python Part 2 — Python code to connect AWS Redshift

In the Part 1 session, we set up the AWS Redshift account to connect to the outer world.

In this session, I will demonstrate some of the code that will be set up and connected to a python world to AWS Redshift.

And my main goal is to upload a pandas DataFrame in AWS Redshift.

Before going to the coding part let's bump into the Query Editor V2.

First logged in AWS Redshift Session or we can go to Redshift Dashboard.

After visiting the Dashboard page we will see the list of clusters on the CLuster Overview Session.

Now we see that I have 2 clusters created one is active which is “Available” and another one is inactive which is “Paused”. I am going to use redshift-cluster-3.

I will also show you how to activate or we can say how to make redshift-cluster-2 Available.

Click “redshift-cluster-2” name or your cluster name, and it will redirect to the cluster page, click on “Action”, and it will show a drop-down menu.

Inside the menu, you will see “Resume”, please click on that. After clicking it takes some more minutes to activate the cluster. So, you will see the status of the cluster on the AWS Redshift cluster dashboard and the message will be Resume Modifying and after some, it will become Available.

Once our cluster is “Available”, now, let's start working with python code.

We are going to install a redshift connector by running the command given below.

pip install redshift-connector

For connecting AWS Redshift via python we require certain credentials like Hostname, Database Name, Port Number, User and Password.

If we are inside our cluster side, we go to Properties, we will see the credentials we are looking for.

Now we are going to run the query in python to create a table on Redshift.

Before that let's create a sample DataFrames


Now we are going to create a table inside the AWS Redshift cluster.

We will see the table is created.

Now we are going to insert DataFrame inside the table.


Now in the query editor, we will see that our table is created and filled with data inside the table.

This whole sample demonstrates how we can create an AWS Redshift cluster and how can I insert pandas DataFrames.

We can upload n number of Data inside DataFrame in Redshift cluster.

Top comments (0)