Re: Import file into bytea field in SQL/plpgsql? - Mailing list pgsql-general

From Erwin Brandstetter
Subject Re: Import file into bytea field in SQL/plpgsql?
Date
Msg-id 24723228-d5db-4c55-a879-2ea4b80cd31e@e23g2000prf.googlegroups.com
Whole thread Raw
In response to Import file into bytea field in SQL/plpgsql?  (Erwin Brandstetter <brsaweda@gmail.com>)
Responses Re: Import file into bytea field in SQL/plpgsql?  ("Douglas McNaught" <doug@mcnaught.org>)
List pgsql-general
On Mar 5, 10:20 am, d...@archonet.com (Richard Huxton) wrote:
> Erwin Brandstetter wrote:
> > Hi!
>
> > What I want to do:
> > Import a file from the file system into a bytea field of a table.
(...)
> Not that I know of. It's simple enough to do from the application side
> of things of course (well, in most languages) but there's no general
> file access.
>
> You can do various tricks to grab text values (see psql in the docs "SQL
> Interpolation") but you'd need to escape the values. Not sure that's any
> cleaner than the large-object approach.


Thanks for the answer. "SQL Interpolation" is interesting (and
surprising) but not exactly clean, as you've implied. And I still
don't see a way to recreate a file from a bytea field other than with
lo_export.

I thought that maybe "COPY tbl(bytea_fld) FROM .. " /  "COPY
tbl(bytea_fld) TO .. " might do the trick, possibly with the BINARY
key word, but I didn't find a way.

The whole concept behind large objects is a bit off. Since we have
TOAST tables, it is of limited use to store large objects away in a
system table. It would be useful to have (additional) functions like:
   lo_import(text) RETURNS bytea
   lo_export(bytea, text) RETURNS integer

So we could import files into bytea fields with:
   INSERT INTO mytable (bytea_fld) VALUES(lo_import('/mypath/
myfile'));
and (re-)create one or more files with:
   SELECT lo_export(bytea_fld, filename_fld) FROM mytable WHERE <some
condition>;

That would probably be easy to implement for someone who knows the
large objects functions and C, i.e. someone who is not me.


Regards
Erwin

pgsql-general by date:

Previous
From: Justin
Date:
Subject: Re: how do you write aggregate function
Next
From: "Douglas McNaught"
Date:
Subject: Re: Import file into bytea field in SQL/plpgsql?