Last active
August 19, 2020 13:43
-
-
Save rakeshsingh/709c700aa78aeff00ca5 to your computer and use it in GitHub Desktop.
Python Redshift Connection using PG8000
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
import json | |
import pg8000 as dbapi | |
from pprint import pprint | |
def getconnection(database,host,port,user,password): | |
conn= None | |
try: | |
conn=dbapi.connect(database=database,host=host, port=port,\ | |
user=user,password=password,ssl=True) | |
except Exception as err: | |
print(err) | |
return conn | |
def runquery(conn,query): | |
""" | |
Just run a query given a connection | |
""" | |
curr=conn.cursor() | |
curr.execute(query) | |
for row in curr.fetchall(): | |
pprint(row) | |
return None | |
if __name__ =='__main__': | |
config={ | |
"database": "databasename", | |
"host": "myhost.redshift.amazonaws.com", | |
"port": 8192, | |
"user": 'username', | |
"password": 'password' | |
} | |
conn = getconnection(config['database'],config['host'],\ | |
config['port'],config['user'],config['password']) | |
runquery(conn,\ | |
''' | |
select datname, nspname, relname | |
from pg_class, pg_namespace, pg_database | |
where pg_namespace.oid = relnamespace | |
and datname ='dwrsg010' | |
and nspname not in | |
('information_schema','pg_catalog','pg_toast') | |
group by datname, nspname, relname | |
order by datname, nspname, relname | |
; | |
''' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sounds like you need: