Skip to content

Instantly share code, notes, and snippets.

@varver
Created February 15, 2017 11:02
Show Gist options
  • Save varver/f6f1ad1a1cfd786f8e374d11fd3dbd4b to your computer and use it in GitHub Desktop.
Save varver/f6f1ad1a1cfd786f8e374d11fd3dbd4b to your computer and use it in GitHub Desktop.
Python script to connect with Redshift on AWS with SCHEMA support.
############ REQUIREMENTS ####################
# sudo apt-get install python-pip
# sudo apt-get install libpq-dev
# sudo pip install psycopg2
# sudo pip install sqlalchemy
# sudo pip install sqlalchemy-redshift
##############################################
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
#>>>>>>>> MAKE CHANGES HERE <<<<<<<<<<<<<
DATABASE = "dbname"
USER = "username"
PASSWORD = "password"
HOST = "host"
PORT = ""
SCHEMA = "public" #default is "public"
####### connection and session creation ##############
connection_string = "redshift+psycopg2://%s:%s@%s:%s/%s" % (USER,PASSWORD,HOST,str(PORT),DATABASE)
engine = sa.create_engine(connection_string)
session = sessionmaker()
session.configure(bind=engine)
s = session()
SetPath = "SET search_path TO %s" % SCHEMA
s.execute(SetPath)
###### All Set Session created using provided schema #######
################ write queries from here ######################
query = "SELECT * FROM added_trip limit 2;"
rr = s.execute(query)
all_results = rr.fetchall()
def pretty(all_results):
for row in all_results :
print "row start >>>>>>>>>>>>>>>>>>>>"
for r in row :
print " ----" , r
print "row end >>>>>>>>>>>>>>>>>>>>>>"
pretty(all_results)
########## close session in the end ###############
s.close()
@sapana-sainee30
Copy link

Thank you very much

@tilfast
Copy link

tilfast commented Sep 27, 2018

Thanks a ton, clean and straightforward

@ericbarber
Copy link

You do great work! Thank you.

@ianstream
Copy link

add connect_args={'sslmode': 'prefer'} to a.create_engine

@9555643857
Copy link

connection issue attached screenshot
image

@varver
Copy link
Author

varver commented Aug 19, 2019 via email

@9555643857
Copy link

yes sir,but connection issue redshift datatbase

image

@ahmed0315
Copy link

ahmed0315 commented Feb 3, 2021

How to inset data into the database using your logic. please provide example in the code. Thank you

@csharm
Copy link

csharm commented Apr 3, 2021

How to inset data into the database using your logic. please provide example in the code. Thank you

query = "INSERT INTO mytable VALUES ('myvalue123');"
rr=s.execute(query)
s.commit()
s.close()

Please change the query appropriately.

@sarangmn
Copy link

sarangmn commented May 15, 2021

I guess if the goal is to query redshift datatable, do you really need session maker? You can use Pandas read_sql after creating the engine instance as shown in the article
df = pd.read_sql('SELECT * FROM ;', engine)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment