Skip to content

Instantly share code, notes, and snippets.

@rakeshsingh
Last active August 19, 2020 13:43
Show Gist options
  • Save rakeshsingh/709c700aa78aeff00ca5 to your computer and use it in GitHub Desktop.
Save rakeshsingh/709c700aa78aeff00ca5 to your computer and use it in GitHub Desktop.
Python Redshift Connection using PG8000
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
;
''' )
@ecdedios
Copy link

Howdy. Decided to try this because it looks like super-simple example - exactly what I need. However, I can't get it to work. Obviously I'm doing something wrong, but no idea what. Any help appreciated.

What am I doing wrong? I imported the pg8000 module with pip into the folder, created this script as lambda_function.py, changed it to all my settings, zipped them all together (pg8000 imported scramp), uploaded, set up a bogus test, and ran it. I have it set as Python 3.8. (None of them worked, they all threw different errors, but mostly similar). The IAM is good and it just appears to be that it's expecting a handler that's not there, but no idea what it should be. I have the Handler set to be "lambda_function.lambda_handler", since that's the default (and changing it to runquery or getconnection both throw different errors)

{
"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'",
"errorType": "Runtime.HandlerNotFound"
}

Sounds like you need:

def lambda_handler(event, context):
    pass
    return

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