Here is a way to connect to RedShift DB using Python library [psycopg2].
psycopg2: A Python-PostgreSQL Database Adapter
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:
Thanks for posting..Helpful
useful, but python doesn't use semicolons at the end of lines.
what is the 'host' for ?
Agreed! Python uses the ; as a separator, not a terminator. As long as doesn't break ..Old habit die hard :)
It was useful, Thanks a lot.
It was helpful
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
Thank you.. This is very helpful. . python Online Training
very nice and great blog with useful information Best python course in ameerpet
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
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
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
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
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
This post is great. I reallly admire your post. Your post was awesome.
data science course in Hyderabad
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
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
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
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
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
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
Post a Comment