#database #postgresql #terminal #tools

To quickly import a CSV file into a PostgreSQL table:

1SET DateStyle = 'ISO, DMY'; -- Set the correct date format
2copy mytable (column1, column2) from 'local-file.csv' delimiter ',' csv header; -- Import the files

If you want, you can also stream from stdin:

1SET DateStyle = 'ISO, DMY'; -- Set the correct date format
2copy mytable (column1, column2) from STDIN delimiter ',' csv header; -- Import the files

You can then run it from the shell like this:

psql -h remotehost -d remote_mydb -U myuser -c \
    "copy mytable (column1, column2) from STDIN with delimiter as ','" \
    < /path/to/local/file.csv