Postgres CSV COPY from/import is not respecting CSV headers

前端 未结 3 634
北恋
北恋 2021-01-04 08:51

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

3条回答
  •  感情败类
    2021-01-04 09:32

    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 <

提交回复
热议问题