Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Created November 28, 2016 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/b4d19b092e5671d28ada4c5ae6f0686c to your computer and use it in GitHub Desktop.
Save jczaplew/b4d19b092e5671d28ada4c5ae6f0686c to your computer and use it in GitHub Desktop.
Auto import csv to Postgres
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)
@jczaplew
Copy link
Author

jczaplew commented Nov 28, 2016

python import_csv.py ~/my-awesome-csv.csv awesome_csv_table

@scheung38
Copy link

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