#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
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.