Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7497543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:08:19+00:00 2026-05-29T19:08:19+00:00

i am working wit h PostgreSQL 9 for an application, i have a database

  • 0

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
enter image description here

can anyone tell me how to use lo_export function from the server machine and create the image on client machine?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:08:21+00:00Added an answer on May 29, 2026 at 7:08 pm

    Basically:

    1. lo_export will instruct the server to write a file locally (always)
    2. \copy will be transformed by psql to a COPY ... TO STDOUT command and the output written to the specified file. (So what is written to that file is the result of the select statment you were doing before)

    So, you can not use lo_export in 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_export psql command which takes an OID and filename, e.g.:

    \lo_export 28914648 testfile
    

    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:

    CREATE OR REPLACE FUNCTION getfiledata(lobjid oid) RETURNS bytea
      STRICT STABLE LANGUAGE plpgsql AS $$
    DECLARE
      fd int4;
      imgsize int4;
      INV_READ int4 := 262144;
      SEEK_SET int4 := 0;
      SEEK_END int4 := 2;
    BEGIN
      SELECT lo_open(lobjid, INV_READ) INTO fd;
      PERFORM lo_lseek(fd, 0, SEEK_END);
      SELECT lo_tell(fd) INTO imgsize;
      PERFORM lo_lseek(fd, 0, SEEK_SET);
      RETURN loread(fd, imgsize);
    END;
    $$;
    

    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 \copy it will be sent to the client.

    It’s generally recommended these days to use bytea columns 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_export
    Try this from commandprompt.com

     `"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h 192.168.1.101 -p 5432 -d myDB -U DB_admin -c  "\lo_export 19135 'C://leeImage.jpeg' ";`
    

    where the number 19135 is the OID of the species whose image i want on the client system.. the OID you can get from the fishes table fishesimages OID
    use the OID in the above code and you can use the OID get the images.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I have a problem wit the software I'm working on. We are accessing Windows
I am at wit's end. I have a working install of python 2.6.5 with
Im working on software that required psqlodbc drivers and postgresSQl 9.0 database, we have
Alright, I'm at my wit's end on this issue. First, backstory. I'm working on
Working on a project at the moment and we have to implement soft deletion
Imagine that I have a C# application that edits text files. The technique employed
I have a working custom login realm (provided by a third-party) for Glassfish 3.1.1.
Working on an image gallery, but it keeps crashing on me (you control it
I'm working on an image slideshow that allows users to cycle through a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.