Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import CSV file data into a PostgreSQL table?

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

like image 750
vardhan Avatar asked Jun 07 '10 06:06

vardhan


People also ask

How do I import a CSV file into PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.


2 Answers

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); 
like image 131
Bozhidar Batsov Avatar answered Sep 28 '22 23:09

Bozhidar Batsov


If you don't have permission to use COPY (which work on the db server), you can use \copy instead (which works in the db client). Using the same example as Bozhidar Batsov:

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' DELIMITER ',' CSV 

Mind that \copy ... must be written in one line and without a ; at the end!

You can also specify the columns to read:

\copy zip_codes(ZIP,CITY,STATE) FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV 

See the documentation for COPY:

Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.

and note:

For identity columns, the COPY FROM command will always write the column values provided in the input data, like the INSERT option OVERRIDING SYSTEM VALUE.

like image 23
bjelli Avatar answered Sep 28 '22 23:09

bjelli