Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy data from mysql dump file to postgres DB
#!/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
You can’t perform that action at this time.