Thursday, January 08, 2015

Importing data from a CSV file into a Postgres table


You have a CSV file called “data.csv”. The values of the different columns are delimited using “,” You want to import this data into Postgres, in a table called 'example'

In postgres execute this command:

COPY example FROM '/path/to/data.csv' WITH DELIMITER ',' CSV HEADER;

If you have in the table the columns user_id, item_id and rating and in your csv you have:

2,5,3.0
2,123, 4.0

Then in your table you will have:

user_id   item_id    rating
2               5           3.0
2              123        4.0

No comments:

Post a Comment