473,413 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

psql exit status varies for scripts on STDIN

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

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

Nov 23 '05 #1
2 4971
"Jeffrey W. Baker" <jw*****@acm.org> writes:
$ 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.


AFAICS the first behavior comports with what it says in the psql man
page, and the second does not.

: Exit Status

: psql returns 0 to the shell if it finished normally, 1 if a fatal error
: of its own (out of memory, file not found) occurs, 2 if the connection
: to the server went bad and the session was not interactive, and 3 if an
: error occurred in a script and the variable ON_ERROR_STOP was set.

I think this behavior is correct for the script case. You can quibble
about whether reading from stdin is just like the script case or not,
but really the answer is to set ON_ERROR_STOP if you want a report.

If -c is considered to be a shorthand for a script, then this ought to
yield either 0 or 3 depending on whether ON_ERROR_STOP is set by
~/.psqlrc. However I'm not sure if ~/.psqlrc is read for a -c
invocation. I'd be willing to agree that -c should act as though
ON_ERROR_STOP is set always ... but in that case the exit code should
be 3, not 1.

Peter, I think you were the last to touch this stuff; any comments?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
Tom Lane wrote:
If -c is considered to be a shorthand for a script, then this ought
to yield either 0 or 3 depending on whether ON_ERROR_STOP is set by
~/.psqlrc. However I'm not sure if ~/.psqlrc is read for a -c
invocation. I'd be willing to agree that -c should act as though
ON_ERROR_STOP is set always ... but in that case the exit code should
be 3, not 1.


-c isn't really like a script, it's just a one-shot command. (You can't
mix SQL and meta-commands, for example.) I think changing the exit
status from 1 to 3 is reasonable.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Daniel Schuchardt | last post by:
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'); ...
7
by: Willem Herremans | last post by:
I am developing a client application for postgreSQL in Tcl/Tk (see http://gborg.postgresql.org/project/pfm ). It mainly uses PgTcl or pgintcl. I don't have any problems with those, but I am also...
4
by: David Garamond | last post by:
Can psql be told to exit immediately after an error (especially when doing commands from a file, -f)? This is the default behaviour of the mysql client, except when we give it -f option ("force")....
6
by: Grainne Reilly | last post by:
I am new to PostgreSQL and am porting some scripts written for MySQL over to psql. There is one MySQL feature which I cannot find in psql - user defined SQL variables. In MySQL I can use these to...
15
by: Dino Vliet | last post by:
Hi folks, probably this is a question you've heard so many times but I wasn't able to find a solution to it. I'm using a shell script to create a textfile for me. It looks like...
2
by: Ron St-Pierre | last post by:
I'm having a problem suppressing output from some of my cron scripts and java code. One file of sql scripts (eod-misc.sql) is called by a shell script (update.sh). Within eod-misc, various sql...
4
by: Kevin Murphy | last post by:
This is a tip for the record in case it helps somebody else in the future. I have an import script that relies on a stored procedure that runs as a trigger on inserts into a temporary table. ...
2
by: Holger Klawitter | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there, I haven't found a way to set the client encoding of a psql connection other then "\encoding" which is arkward for batch processing. As...
5
by: Andrew Greensted | last post by:
Hi All, I've written a simple c program that reads stdin, adds highlighting to particular words and outputs the result to stdout. I invoke it as follows (where xil-highlight is my program): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.