Importing .csv with timestamp column (dd.mm.yyyy hh.mm.ss) using psql \copy

后端 未结 3 500
天涯浪人
天涯浪人 2020-12-03 18:17

I\'m trying to import data from a .csv file into a postgresql 9.2 database using the psql \\COPY command (not the SQL COPY).

The input .csv file contains

3条回答
  •  独厮守ぢ
    2020-12-03 19:02

    I found it difficult to apply 'SET datestyle' within the same session when applying the psql command. Altering the datestyle on the whole database/server (just for the import) also might cause side effects on other users or existing applications. So i usually modify the file itself before loading:

    #!/bin/bash 
    #
    # change from dd.mm.yyyy to yyyy-mm-dd inside the file
    # note: regex searches for date columns separated by semicolon (;) 
    sed -i 's/;\([0-9][0-9]\)\.\([0-9][0-9]\)\.\([0-9][0-9][0-9][0-9]\);/;\3-\2-\1;/g' myfile
    # then import file with date column
    psql  -c "\COPY mytable FROM 'myfile' ...."
    

提交回复
热议问题