If you’re using Postgres as a database and need to import data from a .csv file, it’s really easy to do using psql
.
Before you start, you’l need:
psql
running in an interactive terminal- A Postgres database with a table you want to import data, we’ll call it user_data
- Data in a .csv file
Your .csv file should have matching headers to the column names in your Postgres table
Example file: users.csv
user_id,location,is_active,created_at
10,USA,true,2020-03-08
11,UK,true,5678,2020-03-09
...
To import use the COPY command:
COPY user_data from 'users.csv' WITH DELIMITER ',' CSV HEADER;
Result:
user_id | location | is_active | created_at |
---|---|---|---|
10 | USA | true | 2020-03-08 |
11 | UK | true | 2020-03-09 |