PostgreSQL CSV import from command line

前端 未结 4 1787
予麋鹿
予麋鹿 2020-12-13 09:15

I\'ve been using the psql Postgres terminal to import CSV files into tables using the following

COPY tbname FROM
\'/tmp/the_file.csv\'
delimiter \'|\' csv;
<         


        
4条回答
  •  悲哀的现实
    2020-12-13 09:30

    The most flexible way is to use a shell HERE document, which allows you to use shell variables inside your query, even inside (double or single) quotes:

    #!/bin/sh
    
    THE_USER=moi
    THE_DB=stuff
    THE_TABLE=personnel
    PSQL=/opt/postgresql/bin/psql
    THE_DIR=/tmp
    THE_FILE=the_file.csv
    
    ${PSQL} -U ${THE_USER} ${THE_DB} <

提交回复
热议问题