Re: postgres via shell scripts - Mailing list pgsql-general

From Jim Mercer
Subject Re: postgres via shell scripts
Date
Msg-id 20001006111902.P22260@reptiles.org
Whole thread Raw
In response to Re: postgres via shell scripts  (bmccoy@chapelperilous.net)
Responses Re: postgres via shell scripts  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-general
On Fri, Oct 06, 2000 at 11:17:08AM -0400, bmccoy@chapelperilous.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@reptiles.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.  ]

pgsql-general by date:

Previous
From: bmccoy@chapelperilous.net
Date:
Subject: Re: postgres via shell scripts
Next
From: Peter Eisentraut
Date:
Subject: Re: postgres via shell scripts