Sunday, December 30, 2012

PostgreSQL - Copy CSV to table with psql - part 2


CREATE TABLE tbl_test
(
 a text,
 b text,
 c text
)
Content of test.csv, and it put in /Database folder
a1,b1,c1
a2,b2,c2
a3,b3,c3


Instead of using COPY command directly, you can modify the test.cvs file to include it, as
COPY tbl_test FROM STDIN WITH CSV;
a1,b1,c1
a2,b2,c2
a3,b3,c3

\.

Note that you have to include a carriage return in the last line of the file (after '\.')
Then, run the following command to import

MyComputer$ psql -h localhost -p 5432 -U postgres testdb -f ~/Database/test.csv

No comments:

Post a Comment