Last active
March 9, 2021 07:58
-
-
Save polinabee/0e5d1fd393835e31d1b8ddb2a24508b1 to your computer and use it in GitHub Desktop.
Pandas Dataframe to PostgreSQL Quick Conversion
This file contains 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
import os | |
import psycopg2 | |
from psycopg2.extras import execute_batch | |
import pandas as pd | |
def get_tuples(d): | |
datadict = d.to_dict('records') | |
return [tuple(d.values()) for d in datadict] | |
def insert_data(table, cur): | |
fields = [col for col in table.columns] | |
str_sub = ', '.join(['%s'] * len(fields)) | |
query = f"INSERT INTO {table.name}({', '.join(fields)}) VALUES ({str_sub})" | |
execute_batch(cur, query, get_tuples(table)) | |
###### Example of use: | |
''' | |
password = os.environ.get('DEFAULT_PG_PASS') | |
conn = psycopg2.connect(f'postgresql://postgres:{password}@0.0.0.0:5432/postgres') # replace with your connection string | |
with conn.cursor() as cur: | |
tables_to_insert = [customers, orders, cities, order_items] # list of table objects | |
for table in tables_to_insert: | |
insert_data(table, cur) | |
conn.commit() | |
conn.close() | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment