めも

技術メモとその他

how to get and put pdf in bytea (postgres)

only using sql and sql-command

0)main table to store binary file below

prototype=# \d storedpdf
Table "public.storedpdf"

col name type description
id integer key
name character varying file name
pdf bytea to store files

1)create temporary table

prototype=# create table media(val text);

2)copy hex text stream to table(text)

cat /test/get.pdf | xxd -p | tr -d '\n' | psql -d prototype -c "\COPY media(val)  FROM STDIN"

3)convert text to binary by decode function

prototype=# insert into storedpdf(pdf) select decode(val,'hex') from media;

psql -d prototype -c "\COPY (select pdf from storedpdf ) TO STDOUT" | xxd -r -p>get.pdf