Tuesday, November 04, 2014

Connect RedShift via Python's [psycopg2]

Here is a way to connect to RedShift DB using Python library [psycopg2].
psycopg2: A Python-PostgreSQL Database Adapter

#!/usr/bin/python
import psycopg2
import sys
import pprint
from datetime import date, timedelta

#Connect to RedShift
conn_string = "dbname='DBNAME' port='5439' user='USER' password='PWD' host='REDSHIFT_INSTANCE_NAME.redshift.amazonaws.com'";
print "Connecting to database\n        ->%s" % (conn_string)
conn = psycopg2.connect(conn_string);

cursor = conn.cursor();

#Captures Column Names 
column_names = [];
cursor.execute("Select * from SCHEMA_NAME.TABLE_NAME limit 0;");
column_names = [desc[0] for desc in cursor.description]
all_cols=', '.join([str(x) for x in column_names])
print all_cols;

#NR for this - argument under timedelta can be taken as  int(str(sys.argv[1]))
yest = date.today() - timedelta(1);
yest_str= yest.strftime('%Y-%m-%d');
print "Yesterday was\n        ->%s" % (yest_str)

conn.commit();
conn.close();

21 comments:

Unknown said...

Thanks for posting..Helpful

Rich Gaushell said...

useful, but python doesn't use semicolons at the end of lines.

Unknown said...

what is the 'host' for ?

Sobhan H said...

Agreed! Python uses the ; as a separator, not a terminator. As long as doesn't break ..Old habit die hard :)

nkgupta1989 said...

It was useful, Thanks a lot.

Unknown said...

It was helpful

Unknown said...

Hi, Thanks for this post. This blog contains Connect RedShift via Python. This is very helpful for who wants to learn professional Education. Keep sharing more interesting posts.
AWS Redshift

Unknown said...


Thank you.. This is very helpful. . python Online Training

nivedhitha said...

very nice and great blog with useful information Best python course in ameerpet

Hevo Data Inc said...

Excellent post. I always check this type of blog, and I’m impressed with Extremely useful info, I care for such information a lot. I am exploring this particular info for a long time. Thanks to this blog my exploration has ended. snowflake vs redshift

markson said...

To inquire about the enormous measure of information we need an expert and all around experienced Data Scientist, to end up one of the specialists among the Data Scientist swarm,ExcelR Data Science Courses

Data Science Institute In Banglore said...

I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
Data Science Certification in Bangalore

DataScience Specialist said...

The web site is lovingly serviced and saved as much as date. So it should be, thanks for sharing this with us.
Data Science Course in Bangalore

DataScience Specialist said...

I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place
Data Science Training in Bangalore

EXCELR said...




This post is great. I reallly admire your post. Your post was awesome.
data science course in Hyderabad

Maneesha said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
data science

Tableau Course in Raipur - 360DigiTMG said...

Amazing post found to be very impressive while going through this post. Thanks for sharing and keep posting such an informative content.

360DigiTMG Data Analytics Course

EXCELR said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. data science training in Hyderabad

B.T said...

In colleges, students find it difficult to get individual attention. On the other hand, data science programs are designed in a way that each student gets individual attention. data science course in india

Data Science Course in Bhilai - 360DigiTMG said...

Outstanding blog appreciating your endless efforts in coming up with an extraordinary content. Which perhaps motivates the readers to feel excited in grasping the subject easily. This obviously makes every readers to thank the blogger and hope the similar creative content in future too.

Data Analytics online course

data scientist course said...

I feel appreciative that I read this. It is useful and extremely educational and I truly took in a ton from it. data scientist training and placement