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,753
  • 72
  • 57
  • 129
pjason
  • 155
  • 1
  • 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

3

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,738
  • 21
  • 105
  • 131
MatheusOl
  • 10,870
  • 3
  • 30
  • 28
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,738
  • 21
  • 105
  • 131
Simo Kivistö
  • 4,247
  • 3
  • 38
  • 42