Skip to content

Instantly share code, notes, and snippets.

@tombohub
Created April 18, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tombohub/5653577369b6a22ed7f46fc45a4fe7e2 to your computer and use it in GitHub Desktop.
Save tombohub/5653577369b6a22ed7f46fc45a4fe7e2 to your computer and use it in GitHub Desktop.
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