I'm reading postgres table, extracting data and loading it into a csv file. The issue I have is that I'm able to read up to 5gb TABLE and successfully create a csv file. One of my tables is 35 GB and am unable to create a csv file, and the process is getting killed.
I suspect my dataframe is not able to handle large size.
What can we do to overcome this and create csv files successfully?
def table_to_csv(sql, file_path, dbname,port, user):
"""This function creates a csv file from PostgreSQL with query
"""
try:
conn = psycopg2.connect(dbname=dbname, port=port, user=user)
print("Connecting to Database")
# Get data into pandas dataframe
df = pd.read_sql(sql, conn)
# Write to csv file
df.to_csv(file_path, encoding='utf-8', header = True,doublequote = True, sep=',', index=False)
print("CSV File has been created")
conn.close()
except Exception as e:
print("Error: {}".format(str(e)))
sys.exit(1)