In [1]:
import psycopg2
from getpass import getpass
from pandas import read_sql
In [2]:
pwd = getpass('password')
password········
In [3]:
config = { 'dbname': 'dev', 
           'user':'test',
           'pwd':pwd,
           'host':'tst.cryibvg33r9d.ap-southeast-1.redshift.amazonaws.com',
           'port':'5439'
         }
In [4]:
def create_conn(*args,**kwargs):
    config = kwargs['config']
    try:
        con=psycopg2.connect(dbname=config['dbname'], host=config['host'], 
                              port=config['port'], user=config['user'], 
                              password=config['pwd'])
        return con
    except Exception as err:
        print(err)
In [5]:
con = create_conn(config=config)

Querying the sample data loaded into RedShift previously (http://docs.aws.amazon.com/redshift/latest/gsg/rs-gsg-create-sample-db.html)

In [7]:
df = read_sql("select lastname from users where likerock=True limit 100000", con=con)
In [8]:
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 12380 entries, 0 to 12379
Data columns (total 1 columns):
lastname    12380 non-null object
dtypes: object(1)
memory usage: 193.4+ KB
In [9]:
df.head()
Out[9]:
lastname
0 Hodge
1 Drake
2 Glass
3 Herring
4 Blanchard
In [11]:
con.close()