Question

I have a CVS file with coma separeted string values representing hexadecimal int. Here is a sampling file.csv

7c8e507319bd11219373301ad75fdda8,7586a0c46c6a288f620278c727ae699b,493ab373681cac5f1ea1bf3cd69390fc
00376951c73ab83a673184af6886939f,7586a0c46c6a288f620278c727ae699b,cb3e56042f3385fd68bb682973d529c8
fe4a00d3207bcd6f68e7c9092a7e6bee,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83
be0d0b358d102c10c63332d6dba84c6b,7586a0c46c6a288f620278c727ae699b,5852d834b037be87d31a87c90f3ecdf2
90476f4583fa156e99f43480d2aaeefa,7586a0c46c6a288f620278c727ae699b,5852d834b037be87d31a87c90f3ecdf2
e7cf69f2eabcc56e789378f7c2bb97c5,7586a0c46c6a288f620278c727ae699b,5852d834b037be87d31a87c90f3ecdf2
85d714a1cb30b77f7bd9fe7d08f76a39,7586a0c46c6a288f620278c727ae699b,5852d834b037be87d31a87c90f3ecdf2
e67475d8af00b28d1dc7e2019252a1ad,7586a0c46c6a288f620278c727ae699b,ab752539fe24e0f510f1f4771f214c54
8cbe258b1dd9de63aaf3fd050e86747c,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83
d838591510647bbd9a02c5b6fef7d0fc,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83
4e03c3c98bde65b2c1c1762681f691e1,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83
9e3022eac89f3b708331db61b2804d92,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83
c744553021baa07a1b7ee512a5c0fb89,7586a0c46c6a288f620278c727ae699b,3c45806c8b3b0b25086c0492d7077d83

Since the file can be huge (2 GB) I would like to import them in a table using postgresql COPY command. Here is the table schema

CREATE TABLE IF NOT EXISTS "datas" (
     s BYTEA, 
     p BYTEA, 
     o BYTEA, PRIMARY KEY (s,o)
)

What do I need to fix in this COPY query to do the job ? ?

COPY datas FROM 'file.csv' (DELIMITER(','));

EDITION

I'm also open to MySQL Solutions proposals

Was it helpful?

Solution 2

It works for me if I specify the format:

copy datas from 'file.csv' csv;

select * from datas;
                                 s                                  |                                 p                                  |                                 o                                  
--------------------------------------------------------------------+--------------------------------------------------------------------+--------------------------------------------------------------------
 \x3763386535303733313962643131323139333733333031616437356664646138 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3439336162333733363831636163356631656131626633636436393339306663
 \x3030333736393531633733616238336136373331383461663638383639333966 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x6362336535363034326633333835666436386262363832393733643532396338
 \x6665346130306433323037626364366636386537633930393261376536626565 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
 \x6265306430623335386431303263313063363333333264366462613834633662 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3538353264383334623033376265383764333161383763393066336563646632
 \x3930343736663435383366613135366539396634333438306432616165656661 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3538353264383334623033376265383764333161383763393066336563646632
 \x6537636636396632656162636335366537383933373866376332626239376335 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3538353264383334623033376265383764333161383763393066336563646632
 \x3835643731346131636233306237376637626439666537643038663736613339 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3538353264383334623033376265383764333161383763393066336563646632
 \x6536373437356438616630306232386431646337653230313932353261316164 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x6162373532353339666532346530663531306631663437373166323134633534
 \x3863626532353862316464396465363361616633666430353065383637343763 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
 \x6438333835393135313036343762626439613032633562366665663764306663 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
 \x3465303363336339386264653635623263316331373632363831663639316531 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
 \x3965333032326561633839663362373038333331646236316232383034643932 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
 \x6337343435353330323162616130376131623765653531326135633066623839 | \x3735383661306334366336613238386636323032373863373237616536393962 | \x3363343538303663386233623062323530383663303439326437303737643833
(13 rows)

OTHER TIPS

The COPY postgreSQL format that is close to your input has bytea columns starting with \\x

Example:

 COPY (select 'abc'::bytea) TO stdout;

will output:

 \\x616263

Conversely it would work to import that string with COPY FROM into a bytea field.

Assuming a command line environment with awk, the input can be transformed on the fly into this format and streamed into psql:

awk -F, '{print "\\\\x"$1",\\\\x"$2",\\\\x"$3}' file.csv  | psql -c "COPY datas FROM stdin delimiter ','"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top