Postgresql – How to import CSV file data into a PostgreSQL table

csvpostgresqlpostgresql-copy

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

Best Answer

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);