How to import CSV file data into a PostgreSQL table?

前端 未结 19 2676
再見小時候
再見小時候 2020-11-22 02:14

How can I write a stored procedure that imports data from a CSV file and populates the table?

19条回答
  •  萌比男神i
    2020-11-22 03:00

    You can create a bash file as import.sh (that your CSV format is a tab delimiter)

    #!/usr/bin/env bash
    
    USER="test"
    DB="postgres"
    TBALE_NAME="user"
    CSV_DIR="$(pwd)/csv"
    FILE_NAME="user.txt"
    
    echo $(psql -d $DB -U $USER  -c "\copy $TBALE_NAME from '$CSV_DIR/$FILE_NAME' DELIMITER E'\t' csv" 2>&1 |tee /dev/tty)
    
    

    And then run this script.

提交回复
热议问题