Re: postgres via shell scripts

From: Jim Mercer <jim(at)reptiles(dot)org>
To: bmccoy(at)chapelperilous(dot)net
Cc: Mike Sears <msears(at)vianet(dot)ca>, pgsql-general(at)postgresql(dot)org
Subject: Re: postgres via shell scripts
Date: 2000-10-06 15:19:03
Message-ID: 20001006111902.P22260@reptiles.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Oct 06, 2000 at 11:17:08AM -0400, bmccoy(at)chapelperilous(dot)net wrote:
> On Fri, 6 Oct 2000, Mike Sears wrote:
> > Is there away to access a postgres database using a shell script, like bash
> > or korn?
>
> You can use
>
> psql -c <query>
>
> or
>
> psql -f <filename>
>
> to do command-line stuff inside of a script. But then you have to figure
> out how to parse the result. You're probably better off going the next
> step and using Perl.

i generally use:

psql -A -t -q -d database -c "sql query" > tempfile
if [ $? -ne 0 ] ; then
echo command failed
exit
fi
awk -F\| -f script.awk tempfile
.....

there is a commandline options somewhere to change he field seperator, but
i forget it now.

you could do it all in one line and pipe the psql into awk/whatever.

but i've found that checking the return status of psql before actually using
the data is a useful thing. 8^)

--
[ Jim Mercer jim(at)reptiles(dot)org +1 416 410-5633 ]
[ Reptilian Research -- Longer Life through Colder Blood ]
[ Don't be fooled by cheap Finnish imitations; BSD is the One True Code. ]

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2000-10-06 16:04:05 Re: postgres via shell scripts
Previous Message bmccoy 2000-10-06 15:17:08 Re: postgres via shell scripts