Skip to content

Instantly share code, notes, and snippets.

@zh4n7wm
Created March 29, 2019 12:34
Show Gist options
  • Save zh4n7wm/65026b8953f2a892b141b71605092ee5 to your computer and use it in GitHub Desktop.
Save zh4n7wm/65026b8953f2a892b141b71605092ee5 to your computer and use it in GitHub Desktop.
import MySQL table to PostgreSQL

dump MySQL table

mysql -u root -p <dbname> -e 'select * from <table-name>' > /tmp/<table-name>.csv

format csv file:

TABLE_NAME='<table-name>' python -c "import os, pandas as pd;csv_path='/tmp/{}.csv'.format(os.getenv('TABLE_NAME'));df = pd.read_csv(csv_path, sep='\t');df.to_csv(csv_path, header=True, index=False)"

import to PostgreSQL

create database and table in PostgreSQL.

psql -d <dbname>
dbname=# \copy <table-name>("<filed-a>", "<field-b>", ...) from '/tmp/<table-name>.csv' DELIMITER ',' CSV HEADER

Note: if your table column include upper case letter, you must include column name with double quotation marks.

BTW: I can not import with pgLoader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment