Created
November 28, 2016 03:09
Revisions
-
jczaplew created this gist
Nov 28, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ import csv import sys import subprocess with open(sys.argv[1], 'rb') as csvfile: reader = csv.reader(csvfile) header_row = reader.next() cmd1 = "psql -U you dbname -c 'CREATE TABLE " + sys.argv[2] + " (" for idx, column in enumerate(header_row): cmd1 += column + " text" if idx != len(header_row) - 1: cmd1 += ", " cmd1 += ")'" subprocess.call(cmd1, shell=True) cmd2 = "psql -U you dbname -c \"COPY " + sys.argv[2] + " FROM '" + sys.argv[1] + "' DELIMITER ',' CSV HEADER\"" subprocess.call(cmd2, shell=True)