Import a CSV with PostgreSQL or H2

If you have a CSV with some rows that you want to directly insert into your PostgreSQL database, you can you the command COPY:

COPY table_name FROM 'path/to/file.csv' DELIMITER ' ' CSV;

Here I used a tab as delimiter but you can of course use any other character, probably ‘;‘ or ‘,‘. This PostgreSQL command can only be executed as SuperUser. A workaround if you are not/don’t want to use root, is to use the psql command ‘\copy’ in your terminal. (more)

With H2 you can also import CSVs but with CSVREAD:

  ('path/to/file.csv', null, 'fieldSeparator=' || CHAR(9));

Thi should have the same result. The CHAR(9) is just a tab (9 is its ASCII value)

Laisser un commentaire

Your email address will not be published / Required fields are marked *