Created
November 28, 2016 03:09
-
-
Save jczaplew/b4d19b092e5671d28ada4c5ae6f0686c to your computer and use it in GitHub Desktop.
Auto import csv to Postgres
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 characters
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) |
Hi @jczaplew does this automatically imports a new CSV next time python starts up? as CSV could be uploaded while Python is not running?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python import_csv.py ~/my-awesome-csv.csv awesome_csv_table