Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Database migration using pandas and sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.sql.expression import select
db1_uri = 'db_uri_here'
db2_uri = 'db_uri_here'
db1_engine = create_engine(db1_uri)
db2_engine = create_engine(db2_uri)
db1_conn = db1_engine.connect()
db2_conn = db2_engine.connect()
table = 'table_name_here'
query = select(table_name)
df = pd.read_sql(query, db1_conn)
df.to_sql(table, db2_conn, index=False)
# repeat for each table, change table name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment