pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedpsql dones't reflect exit status if input command via stdin
3+ messages / 2 participants
[nested] [flat]
* psql dones't reflect exit status if input command via stdin
@ 2019-05-14 06:24 magodo <wztdyl@sina.com>
0 siblings, 1 reply; 3+ messages in thread
From: magodo @ 2019-05-14 06:24 UTC (permalink / raw)
To: pgsql-general
Hello,
I found when running command like `# echo "xxx" | psql postgres
postgres`, the return code is always 0 even though the command ("xxx")
here is of invalid syntax. While the `psql -c` way handled exit code
correctly.
In the meanwhile, mysql client handled both cases correctly.
---
Zhaoting.Weng
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: psql dones't reflect exit status if input command via stdin
@ 2019-05-14 06:39 David G. Johnston <david.g.johnston@gmail.com>
parent: magodo <wztdyl@sina.com>
0 siblings, 1 reply; 3+ messages in thread
From: David G. Johnston @ 2019-05-14 06:39 UTC (permalink / raw)
To: magodo <wztdyl@sina.com>; +Cc: pgsql-general
On Mon, May 13, 2019 at 11:24 PM magodo <wztdyl@sina.com> wrote:
> I found when running command like `# echo "xxx" | psql postgres
> postgres`, the return code is always 0 even though the command ("xxx")
> here is of invalid syntax. While the `psql -c` way handled exit code
> correctly.
>
Its only required to handle things as documented, which this is:
"psql returns 0 to the shell if it finished normally, 1 if a fatal error of
its own occurs (e.g. out of memory, file not found), 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."
The pipe-version causes psql to execute stdin as a script. By default
ON_ERROR_STOP is unset. Thus psql finished processing the script normally
and while it encountered an error it continued past the error as opposed to
stopping with exit code 3.
Adding "\set ON_ERROR_STOP 1" to your .psqlrc will result in psql behaving
in the way you expect.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: psql dones't reflect exit status if input command via stdin
@ 2019-05-14 07:24 magodo <wztdyl@sina.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: magodo @ 2019-05-14 07:24 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general
On Mon, 2019-05-13 at 23:39 -0700, David G. Johnston wrote:
> On Mon, May 13, 2019 at 11:24 PM magodo <wztdyl@sina.com> wrote:
> > I found when running command like `# echo "xxx" | psql postgres
> >
> > postgres`, the return code is always 0 even though the command
> > ("xxx")
> >
> > here is of invalid syntax. While the `psql -c` way handled exit
> > code
> >
> > correctly.
>
> Its only required to handle things as documented, which this is:
>
> "psql returns 0 to the shell if it finished normally, 1 if a fatal
> error of its own occurs (e.g. out of memory, file not found), 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."
>
> The pipe-version causes psql to execute stdin as a script. By
> default ON_ERROR_STOP is unset. Thus psql finished processing the
> script normally and while it encountered an error it continued past
> the error as opposed to stopping with exit code 3.
>
> Adding "\set ON_ERROR_STOP 1" to your .psqlrc will result in psql
> behaving in the way you expect.
>
> David J.
Hi David,
Thank you for your quick and excellent answer 😊
---
Zhaoting.Weng
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2019-05-14 07:24 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-05-14 06:24 psql dones't reflect exit status if input command via stdin magodo <wztdyl@sina.com>
2019-05-14 06:39 ` David G. Johnston <david.g.johnston@gmail.com>
2019-05-14 07:24 ` magodo <wztdyl@sina.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox