As well as psycopg2 you need have sqlalchemy and ipython-sql installed in the Notebook server's Python.
Create a file with your Redshift endpoint and credentials:
echo "{
\"user_name\": \"my_user\",
\"password\": \"my_pswd\",
\"host_name\": \"my_cluster_endpoint\",
\"port_num\": \"5439\",
\"db_name\": \"my_db\"
}" > my.creds
Then try this in your notebook:
import sqlalchemy
import psycopg2
import simplejson
%reload_ext sql
%config SqlMagic.displaylimit = 25
with open("my.creds") as fh:
creds = simplejson.loads(fh.read())
connect_to_db = "postgresql+psycopg2://" + \
creds["user_name"] +':'+ creds["password"] +'@'+ \
creds["host_name"] +':'+ creds["port_num"] +'/'+ creds["db_name"];
%sql $connect_to_db
%sql SELECT current_user, version();
Expected output:
'Connected: my_user@my_db'
current_user | version
my_user | PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.24238