1

What is the best way to load CSV data to a table in a PostgreSQL database (in Java)?

Context: I am working on extract, transform, load (ETL) processing - extracted the flat file and generated (csv's of a similar table). I want to load CSV files to a PostgreSQL table in Java.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
pjason
  • 105
  • 2
  • 10
  • In order to load csv data into postgres, you don't need Java. Refer to http://stackoverflow.com/questions/2987433/how-to-import-csv-file-data-into-a-postgres-table – zawhtut Mar 06 '15 at 17:40

2 Answers2

2

The fastest way of loading a CSV file into a PostgreSQL database is using the COPY command.

From the Java side you can use the method copyIn of CopyManager class from the PostgreSQL JDBC driver.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
MatheusOl
  • 9,735
  • 3
  • 27
  • 27
1

In PostgreSQL the usual way of copying data from a CSV file is the COPY statement (more information is in the the PostgreSQL documentation). To use this statement you must have have the file in a location readable by the server.

If the data cannot be put to the server readable location beforehand, you can use a psql \copy (more information) or INSERT statement.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Simo Kivistö
  • 3,992
  • 3
  • 37
  • 40