24

I have several files which are saved as tsv. I want to insert them into a postgresql db, to analyze them with sql.

However, my problem is how to INSERT this tsv files into postgresql 9.2 under windows 7?

I appreciate your reply!

PS.: I have created the table with the right values like:

CREATE TABLE ratings (distribution VARCHAR, votes VARCHAR, rank FLOAT, title VARCHAR);

the file is in the directory:

C:/Users/testUser/Desktop/TSV/ratings.list.tsv

2
  • 1
    tsv as in text search vectors, or did you mean csv as in comma separated values? If the latter, use COPY. Commented Dec 8, 2013 at 16:28
  • @Denis Thx for your answer. By tsv I mean as tab seperated values. Could you show an example for how to use COPY in an sql statement. Commented Dec 8, 2013 at 16:33

4 Answers 4

37

You want something like this:

COPY ratings FROM 'C:/Users/testUser/Desktop/TSV/ratings.list.tsv' DELIMITER E'\t';

The E'\t' is required, otherwise you'll get an error like this:

ERROR: DELIMITER for COPY must be a single one-byte character


If the columns in your TSV don't line up perfectly with your table, you can also define the mapping by doing the following:

COPY ratings (column_1, column_2, ... column_n)
  FROM 'C:/Users/testUser/Desktop/TSV/ratings.list.tsv'
  DELIMITER E'\t';
Sign up to request clarification or add additional context in comments.

2 Comments

if you add CSV to the end, you can also use the HEADER flag
What if the .tsv file specifies a value for a UNIQUE column which already exists in the table?
25

For tab separated values, you can use COPY:

http://www.postgresql.org/docs/current/static/sql-copy.html

Depending on the exact format of your file, it could be something like:

COPY ratings FROM 'C:/Users/testUser/Desktop/TSV/ratings.list.tsv' DELIMITER '\t'

7 Comments

Thx for your answer. I immediately tried it, but got: ERROR: DELIMITER for COPY must be a single one-byte character
ok solved it I had to type a tab instead using \tab. Thx for your help!
\t - tab is default, so in this case, you don't need use DELIMITER
try double-quotes instead of single
I've tried everything mentioned above, but what worked for me was adding (FORMAT CSV, DELIMITER E'\t', HEADER true) after the 'FROM' clause. Hope this helps anyone.
|
2

I'm able to do this with csvsql from csvkit.

To read a TSV (or CSV) and create/insert it to a table, my command line script looks like this:

csvsql --insert input.tsv  --table table_t --tabs --no-constraints --db postgresql://user:passwd@localhost/mydb

Comments

0

Sad to say, but the easiest way is to convert the TSV to a CSV. Most of the built in Postgres import utilities to do things like converting empty strings to nulls, skipping headers, etc are Only for CSV.

See this simple 6 line Python answer on SO. I use it and then CSV loaded like normal without a problem in Postgres after trying for and hour to load a TSV.

2 Comments

Thanks for the download vote. For various use cases the upvoted answer won't work, hence the above suggestion. ;)
I beg to differ; the default postgresql COPY command defaults to TSV, and I see no difference in support for CSV and TSV. Specifically, both of the commands you mention are present in the configuration for TSV file reading, per this page: postgresql.org/docs/9.6/static/sql-copy.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.