I\'m trying to import data from CSV into the table. The issue is that even with CSV HEADER
, the CSV is being imported based on the column index, not on the head
Just to answer Jonathan's comment under the accepted answer - if you want to load the data from CSV "respecting" the column order (I had a few dumps with different schema migration history, or missing columns, which I wanted to import).
If you want to use the CSV headers to import it in Bash:
(my table's name is alarms
)
#!/bin/bash
if [ -z "$1" ] ; then
echo "Usage: $0 "
exit
fi
columns=$(head -n1 $1)
echo "Using columns:"
if ! echo $columns | grep '^id,' ; then
echo "Missing id in header. No header present? See below:"
echo $columns
exit
fi
sudo -u postgres psql YOUR_DATABASE <