sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Daniel Schuchardt's Avatar

psql and blob


Question posted by: Daniel Schuchardt (Guest) on November 11th, 2005 11:53 PM
Hi @ all,

i'm sure there was a psql-function to transfere my Blob-Data to the
server but I can't remember.

I have a script like this :

UPDATE xy SET z = lo_import('localpath_and_file');

but i want to execute this script from the client and so my blob-data is
on the client and lo_import fails (the server doesn't have this file).
Hm.

Thanks for tips - Daniel.


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

15 Answers Posted
Cornelia Boenigk's Avatar
Cornelia Boenigk November 11th, 2005 11:53 PM
Guest - n/a Posts
#2: Re: psql and blob

Hi everybody

To convert PostgreSQL timestmp to UNIX timestamp I use

select ((today::abstime)::int4) from table ...

In the documentation there is a notice that abstime shouldn't be used.
Is there another way to convert?

Also I tried

select timetz_hash(today) from table ..

and got negative values for some timestamps. Does anybody know why?

RedHat 7.3, PostgreSQL 7.2.1


Regards
Conni


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Darko Prenosil's Avatar
Guest - n/a Posts
#3: Re: psql and blob

On Wednesday 17 September 2003 18:32, Daniel Schuchardt wrote:[color=blue]
> Hi @ all,
>
> i'm sure there was a psql-function to transfere my Blob-Data to the
> server but I can't remember.
>
> I have a script like this :
>
> UPDATE xy SET z = lo_import('localpath_and_file');
>
> but i want to execute this script from the client and so my blob-data is
> on the client and lo_import fails (the server doesn't have this file).
> Hm.
>
> Thanks for tips - Daniel.
>[/color]
http://www.postgresql.org/docs/7.3/...rgeobjects.html
is what You need.

Regards !

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Tom Lane's Avatar
Guest - n/a Posts
#4: Re: psql and blob

"Cornelia Boenigk" <poppcorn@cornelia-boenigk.de> writes:[color=blue]
> To convert PostgreSQL timestmp to UNIX timestamp I use
> select ((today::abstime)::int4) from table ...
> In the documentation there is a notice that abstime shouldn't be used.
> Is there another way to convert?[/color]

"SELECT EXTRACT(EPOCH FROM timestamp)" is the recommended way to do it.
[color=blue]
> Also I tried
> select timetz_hash(today) from table ..
> and got negative values for some timestamps. Does anybody know why?[/color]

Why not? If the hash function didn't use all 32 available bits, I'd
think it faulty ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Daniel Schuchardt's Avatar
Daniel Schuchardt November 11th, 2005 11:53 PM
Guest - n/a Posts
#5: Re: psql and blob

Hm, but lo_import/lo_export imports/exports from the local filesystem.
I want to upload/download a clientfile to/from the Server Database with
psql. I think this is a feature in psql. But i can't remember...

Daniel



-----Ursprüngliche Nachricht-----
Von: pgsql-general-owner+M49349=daniel_schuchardt=web.de@postgresql.o rg
[mailto:pgsql-general-owner+M49349=daniel_schuchardt=web.de@postgresql.o
rg] Im Auftrag von Darko Prenosil
Gesendet: Mittwoch, 17. September 2003 20:07
An: Daniel Schuchardt; Join Bytes!
Betreff: Re: [GENERAL] psql and blob


http://www.postgresql.org/docs/7.3/...rgeobjects.html
is what You need.

Regards !

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to Join Bytes! so that your
message can get through to the mailing list cleanly

CoL's Avatar
Guest - n/a Posts
#6: Re: psql and blob

Hi,

if you, then write a program for that. psql is a database server, not a
client program.

C.

Daniel Schuchardt wrote, On 9/17/2003 8:37 PM:[color=blue]
> Hm, but lo_import/lo_export imports/exports from the local filesystem.
> I want to upload/download a clientfile to/from the Server Database with
> psql. I think this is a feature in psql. But i can't remember...
>
> Daniel
>[/color]

Nigel J. Andrews's Avatar
Nigel J. Andrews November 11th, 2005 11:53 PM
Guest - n/a Posts
#7: Re: psql and blob

On Wed, 17 Sep 2003, Daniel Schuchardt wrote:
[color=blue]
> Hm, but lo_import/lo_export imports/exports from the local filesystem.
> I want to upload/download a clientfile to/from the Server Database with
> psql. I think this is a feature in psql. But i can't remember...[/color]


Yes it is. You want to look up \lo_import in the help/manpage/manual of psql.


Nigel Andrews



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to Join Bytes! so that your
message can get through to the mailing list cleanly

Jonathan Bartlett's Avatar
Jonathan Bartlett November 11th, 2005 11:53 PM
Guest - n/a Posts
#8: Re: psql and blob

> if you, then write a program for that. psql is a database server, not a[color=blue]
> client program.[/color]

No, psql is a client program. postmaster is the database server.

Jon

[color=blue]
>
> C.
>
> Daniel Schuchardt wrote, On 9/17/2003 8:37 PM:[color=green]
> > Hm, but lo_import/lo_export imports/exports from the local filesystem.
> > I want to upload/download a clientfile to/from the Server Database with
> > psql. I think this is a feature in psql. But i can't remember...
> >
> > Daniel
> >[/color]
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
>[/color]


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

CoL's Avatar
Guest - n/a Posts
#9: Re: psql and blob

hi,
you are right: I wrote it in wrong way. psql is a client program, but he
wants to get the file not from the place where psql runs.

He says:
" but i want to execute this script from the client and so my blob-data
is on the client and lo_import fails (the server doesn't have this file)."
That's why he needs a program, an application, or something which can
communicate with client. Or another way, if he runs the psql from client
where the file is, than connecting to postgres server :)

Sorry for my ambiguous letter.

C.

Jonathan Bartlett wrote, On 9/17/2003 9:48 PM:
[color=blue][color=green]
>> if you, then write a program for that. psql is a database server, not a
>> client program.[/color]
>
> No, psql is a client program. postmaster is the database server.
>
> Jon[/color]

Doug McNaught's Avatar
Guest - n/a Posts
#10: Re: psql and blob

Daniel Schuchardt <daniel_schuchardt@web.de> writes:
[color=blue]
> Hi @ all,
>
> i'm sure there was a psql-function to transfere my Blob-Data to the
> server but I can't remember.[/color]

The psql function to use is \lo_import--this reads the file from the
client side.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Darko Prenosil's Avatar
Guest - n/a Posts
#11: Re: psql and blob

On Wednesday 17 September 2003 20:37, Daniel Schuchardt wrote:[color=blue]
> Hm, but lo_import/lo_export imports/exports from the local filesystem.
> I want to upload/download a clientfile to/from the Server Database with
> psql. I think this is a feature in psql. But i can't remember...
>
> Daniel
>[/color]
I'm afraid that You did not understand me.
In that documentation are *all* the ways to import and export large objects.
Check lo_write/lo_read and You'll see that they not depend on server
filesystem.

Regards !

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Daniel Schuchardt's Avatar
Daniel Schuchardt November 11th, 2005 11:54 PM
Guest - n/a Posts
#12: Re: psql and blob

Yes thats it. Thanks.

Am Mi, 2003-09-17 um 22.42 schrieb Doug McNaught:[color=blue]
> Daniel Schuchardt <daniel_schuchardt@web.de> writes:
>[color=green]
> > Hi @ all,
> >
> > i'm sure there was a psql-function to transfere my Blob-Data to the
> > server but I can't remember.[/color]
>
> The psql function to use is \lo_import--this reads the file from the
> client side.
>
> -Doug
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings[/color]


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Daniel Schuchardt's Avatar
Daniel Schuchardt November 11th, 2005 11:54 PM
Guest - n/a Posts
#13: Re: psql and blob

Yes thats it.

Ok - one last question.
My Script looks like this and actually i can run it only on the server (so i have to copy all my data to the server each time i want to update my blobs):

INSERT INTO tablexy (BLOBFIELD) VALUES (lo_import('BLOBFILE')).

Now we know if I want to upload a clientfile I have to use \lo_import BUT i cant use this inside the script.

INSERT INTO tablexy (BLOBFIELD) VALUES (\lo_import('BLOBFILE')). is not possible because \lo_import is a unknown command for the server.

So I have to do 2 steps manually in psql:

\lo_import(ClientFile) -> Returns OID

INSERT INTO tablexy (BLOBFIELD) VALUES (Returned OID)

Is there a way to do this automatically?
Means my Clientside script should upload a local file (from the same computer where the script is executed) to the server and insert this file in a special table automatically.

Thanks


Am Mi, 2003-09-17 um 22.42 schrieb Doug McNaught:[color=blue]
> Daniel Schuchardt <daniel_schuchardt@web.de> writes:
>[color=green]
> > Hi @ all,
> >
> > i'm sure there was a psql-function to transfere my Blob-Data to the
> > server but I can't remember.[/color]
>
> The psql function to use is \lo_import--this reads the file from the
> client side.
>
> -Doug
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings[/color]


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Tom Lane's Avatar
Guest - n/a Posts
#14: Re: psql and blob

Daniel Schuchardt <daniel_schuchardt@web.de> writes:[color=blue]
> So I have to do 2 steps manually in psql:
> \lo_import(ClientFile) -> Returns OID
> INSERT INTO tablexy (BLOBFIELD) VALUES (Returned OID)[/color]
[color=blue]
> Is there a way to do this automatically?[/color]

See psql's :LASTOID (I think that's the name) variable.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Darko Prenosil's Avatar
Guest - n/a Posts
#15: Re: psql and blob


----- Original Message -----
From: "Daniel Schuchardt" <daniel_schuchardt@web.de>
To: "Doug McNaught" <doug@mcnaught.org>
Cc: <pgsql-general@postgresql.org>
Sent: Thursday, September 18, 2003 4:44 PM
Subject: Re: [GENERAL] psql and blob

[color=blue]
> Yes thats it.
>
> Ok - one last question.
> My Script looks like this and actually i can run it only on the server (so[/color]
i have to copy all my data to the server each time i want to update my
blobs):[color=blue]
>
> INSERT INTO tablexy (BLOBFIELD) VALUES (lo_import('BLOBFILE')).
>
> Now we know if I want to upload a clientfile I have to use \lo_import BUT[/color]
i cant use this inside the script.[color=blue]
>
> INSERT INTO tablexy (BLOBFIELD) VALUES (\lo_import('BLOBFILE')). is not[/color]
possible because \lo_import is a unknown command for the server.[color=blue]
>
> So I have to do 2 steps manually in psql:
>
> \lo_import(ClientFile) -> Returns OID
>
> INSERT INTO tablexy (BLOBFIELD) VALUES (Returned OID)
>
> Is there a way to do this automatically?
> Means my Clientside script should upload a local file (from the same[/color]
computer where the script is executed) to the server and insert this file in
a special table automatically.

Maybe Your problem is only to find last inserted oid ?
See: http://www.postgresql.org/docs/7.3/...e/app-psql.html and look
for LASTOID.
Part from docs:

LASTOID
The value of the last affected OID, as returned from an INSERT or lo_insert
command. This variable is only guaranteed to be valid until after the result
of the next SQL command has been displayed.


So solution might be :
\lo_import(ClientFile)
INSERT INTO tablexy (BLOBFIELD) VALUES (:LASTOID)
I'm not shure what exactly you want to acomplish, but this might work.

I repeat: lo_read/lo_write from libpq are the only true "client side" way I
know. You can write small "C" program that reads the file from local
filesystem and writes it directly to sql server with no "uploads" or nothing
like that.
Look at : http://www.postgresql.org/docs/7.3/...e/lo-libpq.html
Note that \lo_import and \lo_export PSQL INSTRUCTIONS are using the same
technique, and they act different than server side lo_import() and
lo_export() SERVER SIDE FUNCTIONS. See:
http://www.postgresql.org/docs/7.3/...e/app-psql.html
Hope this helps.

Regards !



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to Join Bytes! so that your
message can get through to the mailing list cleanly

Daniel Schuchardt's Avatar
Daniel Schuchardt November 11th, 2005 11:55 PM
Guest - n/a Posts
#16: Re: psql and blob

Am Do, 2003-09-18 um 20.21 schrieb Darko Prenosil:
[color=blue]
> ----- Original Message -----
> From: "Daniel Schuchardt" <daniel_schuchardt@web.de>
> To: "Doug McNaught" <doug@mcnaught.org>
> Cc: <pgsql-general@postgresql.org>
> Sent: Thursday, September 18, 2003 4:44 PM
> Subject: Re: [GENERAL] psql and blob
>
>[color=green]
> > Yes thats it.
> >
> > Ok - one last question.
> > My Script looks like this and actually i can run it only on the server (so[/color]
> i have to copy all my data to the server each time i want to update my
> blobs):[color=green]
> >
> > INSERT INTO tablexy (BLOBFIELD) VALUES (lo_import('BLOBFILE')).
> >
> > Now we know if I want to upload a clientfile I have to use \lo_import BUT[/color]
> i cant use this inside the script.[color=green]
> >
> > INSERT INTO tablexy (BLOBFIELD) VALUES (\lo_import('BLOBFILE')). is not[/color]
> possible because \lo_import is a unknown command for the server.[color=green]
> >
> > So I have to do 2 steps manually in psql:
> >
> > \lo_import(ClientFile) -> Returns OID
> >
> > INSERT INTO tablexy (BLOBFIELD) VALUES (Returned OID)
> >
> > Is there a way to do this automatically?
> > Means my Clientside script should upload a local file (from the same[/color]
> computer where the script is executed) to the server and insert this file in
> a special table automatically.
>
> Maybe Your problem is only to find last inserted oid ?
> See: http://www.postgresql.org/docs/7.3/...e/app-psql.html and look
> for LASTOID.
> Part from docs:
>
> LASTOID
> The value of the last affected OID, as returned from an INSERT or lo_insert
> command. This variable is only guaranteed to be valid until after the result
> of the next SQL command has been displayed.
>
>
> So solution might be :
> \lo_import(ClientFile)
> INSERT INTO tablexy (BLOBFIELD) VALUES (:LASTOID)
> I'm not shure what exactly you want to acomplish, but this might work.
>
> I repeat: lo_read/lo_write from libpq are the only true "client side" way I
> know. You can write small "C" program that reads the file from local
> filesystem and writes it directly to sql server with no "uploads" or nothing
> like that.
> Look at : http://www.postgresql.org/docs/7.3/...e/lo-libpq.html
> Note that \lo_import and \lo_export PSQL INSTRUCTIONS are using the same
> technique, and they act different than server side lo_import() and
> lo_export() SERVER SIDE FUNCTIONS. See:
> http://www.postgresql.org/docs/7.3/...e/app-psql.html
> Hope this helps.
>
> Regards ![/color]


Ok i will try this. Thats my aim :

I have a masterdatabase located on a computer in our firm. All chages
and so are located in this db. This means also icons/pictures my
application uses. So this data changes during the time because of
development. If we install our app on a new server or update the version
I have 2 scripts. First makes a dump. So I dump my administrative tables
and blobs in our firm and than i want to upload / install the dumped
information to the customers server. Actually this is ugly because of
the local pathes. So I have to upload my dump (it consists of seperate
text (dump as INSERT WITH COLUMN NAMES) and blob files to the server to
reload the data. My scripts look like this :


to prepare

pg_dump -i -h $1 -a -D -t mainmenu -f "/Sql/Dump/ZZ_7 MainMenu.sql" $2
pg_dump -i -h $1 -a -D -t reports -f "/Sql/Dump/ZZ_10 Reports.sql" $2

psql -h $1 -c "SELECT lo_export(mm_picture,
'/Sql/Dump/Blobs/mainmenu.mm_id.'|| CAST(mm_id AS VARCHAR) || '.blob')
FROM mainmenu" $2
psql -h $1 -c "SELECT lo_export(r_blob,
'/Sql/Dump/Blobs/reports.r_id.'|| CAST(r_id AS VARCHAR) || '.blob') FROM
reports WHERE r_blob IS NOT NULL" $2

to reload

psql -h $1 -c "DELETE FROM mainmenu;" $2
psql -h $1 -c "DELETE FROM reports;" $2

psql -h $1 -f "/var/tmp/dump/ZZ_7 MainMenu.sql" $2
psql -h $1 -f "/var/tmp/dump/ZZ_10 Reports.sql" $2

psql -h $1 -c "UPDATE mainmenu SET
mm_picture=lo_import('/var/tmp/dump/Blobs/mainmenu.mm_id.'|| CAST(mm_id
AS VARCHAR) || '.blob') WHERE mm_picture IS NOT NULL" $2
psql -h $1 -c "UPDATE reports SET
r_blob=lo_import('/var/tmp/dump/Blobs/reports.r_id.'|| CAST(r_id AS
VARCHAR) || '.blob') WHERE r_blob IS NOT NULL" $2

perhaps I should try to dump / reload with oid's

 
Not the answer you were looking for? Post your question . . .
196,840 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,840 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors