How can I retrieve the data (including column names) from Amazon Redshift?
!pip install psycopg2-binary
import psycopg2
import numpy as np
import pandas as pd
connection = psycopg2.connect(
host=conn['host'],
database=conn['database'],
user=conn['user'],
password=conn['password'],
sslmode=conn['sslmode'],
port= conn['port'])
cursor = connection.cursor()
cursor.execute("SELECT * FROM <schema>.<table> LIMIT 5;")
data = np.array(cursor.fetchall())
cursor.close()
connection.close()
df = pd.DataFrame(data=data, )
df.head()
My current approach retrieves the data correctly but misses the header of the table, i.e. the column names. I know how to achieve this with sqlalchemy-redshift
I would like to know whereas using plain Psycopg
this is also possible.