How to import CSV file data into a PostgreSQL table?

vardhan picture vardhan · Jun 7, 2010 · Viewed 836.5k times · Source

How can I write a stored procedure that imports data from a CSV file and populates the table?

Answer

Bozhidar Batsov picture Bozhidar Batsov · Jun 7, 2010

Take a look at this short article.


Solution paraphrased here:

Create your table:

CREATE TABLE zip_codes 
(ZIP char(5), LATITUDE double precision, LONGITUDE double precision, 
CITY varchar, STATE char(2), COUNTY varchar, ZIP_CLASS varchar);

Copy data from your CSV file to the table:

COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' WITH (FORMAT csv);