Created
          April 18, 2021 13:12 
        
      - 
      
- 
        Save tombohub/5653577369b6a22ed7f46fc45a4fe7e2 to your computer and use it in GitHub Desktop. 
    Database migration using pandas and sqlalchemy
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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