psql exit status varies for scripts on STDIN - Mailing list pgsql-general

From Jeffrey W. Baker
Subject psql exit status varies for scripts on STDIN
Date
Msg-id 1095137416.4060.1.camel@localhost.localdomain
Whole thread Raw
Responses Re: psql exit status varies for scripts on STDIN  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
I just noticed something unexpected when using psql.  I had written a
shell script to bulk-load some hundreds of files into a database, and
move each file to success/ or failure/ directories depending on the exit
status.

The problem is psql exit status differs depending on if the SQL script
is provided with -c or on STDIN.  Try this for example:

$ echo "your mom" | psql
ERROR:  syntax error at or near "your" at character 1
$ echo $?
0

The script failed, but the exit status is 0 (success) anyway.

$ psql -c "your mom"
ERROR:  syntax error at or near "your" at character 1
$ echo $?
1

If the same command is given with -c, the exit status indicates failure.

The workaround in this case was to provide "COPY table FROM STDIN;" as
the -c COMMAND and to pipe the data in on STDIN.  But simply adding the
COPY statement to the top of the input doesn't work right, with respect
to the exit status.

psql should probably give the same exit status in this case.  Do you
agree?

-jwb

pgsql-general by date:

Previous
From: Chester Kustarz
Date:
Subject: Re: Corrupted Data?
Next
From: Tom Lane
Date:
Subject: Re: how to use geometric shapes?