-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.sh
52 lines (44 loc) · 1.08 KB
/
upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# takes a csv and copies it to a postgres table
# drop existing table
# creates new table
usage() { echo "Usage: " 1>&2; exit 1; }
while getopts ":h:d:s:t:u:p:f:" o; do
case "${o}" in
h)
h=${OPTARG}
;;
d)
d=${OPTARG}
;;
s)
s=${OPTARG}
;;
t)
t=${OPTARG}
;;
u)
u=${OPTARG}
;;
p)
p=${OPTARG}
;;
f)
f=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
export PGPASSWORD=${p};
echo ${p}
# generate create table statement for postgres
csvsql -i postgresql --tables ${t} --db-schema ${s} -d ',' "${f}" > "temp.sql"
# drop table if it already exists
psql -h ${h} -U ${u} -d ${d} -c "DROP TABLE IF EXISTS ${t};"
# create postgres table
psql -h ${h} -U ${u} -d ${d} < "temp.sql"
# copy file to postgres table
cat "${f}" | psql -h ${h} -U ${u} -d ${d} -c "\COPY ${s}.${t} FROM STDIN WITH CSV HEADER DELIMITER ',';"