Created
January 19, 2016 17:25
-
-
Save chanux/7b34b86848f0eb4d3ad5 to your computer and use it in GitHub Desktop.
Copy data from mysql dump file to postgres DB
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
#!/usr/bin/env python | |
import argparse | |
import psycopg2 | |
""" | |
Script to help copy data to postgres databases from mysql dump files | |
mysql2postgres -f <mysql dump file> -t <postgres table name> -c <comma separated list of columns> | |
NOTE: the column name list should be in order of dump file fields | |
""" | |
# Update follwing with information for your postgres database | |
DB_NAME = 'sherminator' | |
DB_USER = 'postgres' | |
DB_HOST = 'localhost' | |
DB_PASS = 'pass' | |
def copy_data(filename, table, columns): | |
try: | |
conn = psycopg2.connect( | |
"dbname='%s' user='%s' host='%s' password='%s'" % ( | |
DB_NAME, | |
DB_USER, | |
DB_HOST, | |
DB_PASS | |
) | |
) | |
except: | |
print "Cannot connect to the DB" | |
with open(filename, 'r') as f: | |
cur = conn.cursor() | |
cur.copy_from(f, table, columns=columns) | |
conn.commit() | |
cur.close() | |
conn.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', dest='filename', required=True, help='Input File: Tab separated file') | |
parser.add_argument('-t', dest='table', required=True, help='Destination table name') | |
parser.add_argument('-c', dest='columns', required=True, help='Comma separated list of destination table columns in order') | |
args = parser.parse_args() | |
columns = tuple(args.columns.strip(',').split(',')) | |
copy_data(args.filename, args.table, columns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment