Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created May 4, 2020 22:28
Show Gist options
  • Save erikerlandson/cd7b682449d711d90d9f6737461aafb7 to your computer and use it in GitHub Desktop.
Save erikerlandson/cd7b682449d711d90d9f6737461aafb7 to your computer and use it in GitHub Desktop.
writing pandas data into postgresql using sqlalchemy
import pandas as pd
data = pd.DataFrame(list(range(100)), columns=["data"])
pip install psycopg2
import sqlalchemy
from sqlalchemy import create_engine
import psycopg2
cxstr = 'postgresql://{user}:{passwd}@{host}:5432/{db}'.format( \
user='xxx', \
passwd='xxx', \
host='postgresql', \
db='demodb')
engine = create_engine(cxstr)
data.to_sql('demo', engine, if_exists='replace', index = False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment