Created
October 27, 2015 17:54
-
-
Save bryan-lott/4c93c60e6af43319b184 to your computer and use it in GitHub Desktop.
Example on how to connect to redshift using psycopg2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'fbaldo' | |
import psycopg2 | |
import pprint | |
configuration = { 'dbname': 'database_name', | |
'user':'user_name', | |
'pwd':'user_password', | |
'host':'redshift_endpoint', | |
'port':'redshift_password' | |
} | |
def create_conn(*args,**kwargs): | |
config = kwargs['config'] | |
try: | |
conn=psycopg2.connect(dbname=config['dbname'], host=config['host'], port=config['port'], user=config['user'], password=config['pwd']) | |
except Exception as err: | |
print err.code, err | |
return conn | |
def select(*args,**kwargs): | |
# need a connection with dbname=<username>_db | |
cur = kwargs['cur'] | |
try: | |
# retrieving all tables in my search_path | |
cur.execute("""select tablename from pg_table_def""") | |
except Exception as err: | |
print err.code,err | |
rows = cur.fetchall() | |
for row in rows: | |
print row | |
print 'start' | |
conn = create_conn(config=configuration) | |
cursor = conn.cursor() | |
print 'start select' | |
select(cur=cursor) | |
print 'finish' | |
cursor.close() | |
for n in conn.notices(): | |
pprint(n) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment