Pre-requisite
You need psycopg2 adapter for Python which is compatible with any PostgreSQL database
To install psycopg2
$ sudo python3.7 -m pip install psycopg2
Sometimes it gives an error when you install from the source, in that case you can install from the binary
$ sudo python3.7 -m pip install psycopg2-binary
import psycopg2
#Obtaining the connection to RedShift
con=psycopg2.connect(dbname= 'dev', host='your-redshift-cluster.redshift.amazonaws.com',
port= '5439', user= 'YourUser', password= 'YourPassword')
#Opening a cursor and running a query
cur = con.cursor()
cur.execute("SELECT SYSDATE;")
#Printing the output
print(cur.fetchall())
cur.close()
con.close()
Output:
[(datetime.datetime(2019, 4, 18, 23, 57, 0, 289196),)]