i am working wit h PostgreSQL 9 for an application, i have a database with a table ‘species’
where i store fish species details along with the image of the species.
the table is
CREATE TABLE fishes
(
fishes character varying(7) NOT NULL,
speciesimages oid,
CONSTRAINT species_pkey PRIMARY KEY (species)
)
WITH (
OIDS=TRUE
);
i use
INSERT INTO species(fishes,fishesimages VALUES('01',lo_import('C://01.jpg'));
To store the images in the database.
to retrieve the images i use
SELECT lo_export(fishes.fishesimages,'c://outimage.jpg')
FROM fishes
WHERE fishes= '01';
This works fine when the host is Localhost but when it is the server i cannot use the path c:// as this may not exist on the server system and i dont have permissions anyways.
so i set out to use
\COPY
like this
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h 192.168.1.101 -p 5432 -d myDB -U DB_admin -c "\COPY (SELECT lo_export(fishes.fishesimages,'01.jpg') FROM fishes WHERE species = '01') TO 'C://leeImage.jpeg' WITH BINARY";
but this create a image file but when i open it its invalid image

can anyone tell me how to use lo_export function from the server machine and create the image on client machine?
Basically:
lo_exportwill instruct the server to write a file locally (always)\copywill be transformed by psql to aCOPY ... TO STDOUTcommand and the output written to the specified file. (So what is written to that file is the result of theselectstatment you were doing before)So, you can not use
lo_exportin this way, it will always write a file onto the server’s filesystem.Of course, you can solve this simply by having the server write to a shared drive, and then read the file from that drive. Ugly, but effective IMHO.
For some recent versions of psql (not sure when this was introduced) there is a
\lo_exportpsql command which takes an OID and filename, e.g.:However you need to get the OID of the file into the script somehow…
You can write a PL/PGsql function like this to dump the file as a bytea:
Now calling this function with the OID of the large object will return its content as a bytea value. You can thus call this function in a COPY command and it will return the file data… and by using
\copyit will be sent to the client.It’s generally recommended these days to use
byteacolumns directly rather than this large object interface (bytea was introduced a lot later). PostgreSQL will automatically move large values into out-of-line storage (“TOAST tables”) and will also compress them (unless the storage mode is set to “external” to suppress this, which is probably the right thing to do for JPEG images etc)EDIT :
\lo_exportTry this from commandprompt.com
where the number
19135is the OID of the species whose image i want on the client system.. the OID you can get from thefishestablefishesimages OIDuse the OID in the above code and you can use the OID get the images.