Thread: PSQL return coder

PSQL return coder

From
James Sewell
Date:
Hello,

I am using PSQL to run SQL from a file with the -f flag as follows:

BEGIN
SQL
SQL
...
END

This gives me rollback on error and a nicer output than -1. This works fine.

My question is in a rollback scenario is it possible to get PSQL to return a non 0 exit status? 

Cheers,a
James



--

James Sewell,
PostgreSQL Team Lead / Solutions Architect
______________________________________
 

Level 2, 50 Queen St, Melbourne VIC 3000

(+61) 3 8370 8000  W www.lisasoft.com  (+61) 3 8370 8099
 



The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

Re: PSQL return coder

From
Pavel Stehule
Date:
Hello


2013/10/10 James Sewell <james.sewell@lisasoft.com>
Hello,

I am using PSQL to run SQL from a file with the -f flag as follows:

BEGIN
SQL
SQL
...
END

This gives me rollback on error and a nicer output than -1. This works fine.

My question is in a rollback scenario is it possible to get PSQL to return a non 0 exit status? 

probably not - from psql perspective all statements was perfect

Pavel
 

Cheers,a
James



--

James Sewell,
PostgreSQL Team Lead / Solutions Architect
______________________________________
 

Level 2, 50 Queen St, Melbourne VIC 3000

(+61) 3 8370 8000  W www.lisasoft.com  (+61) 3 8370 8099
 



The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.


Re: PSQL return coder

From
Tom Lane
Date:
James Sewell <james.sewell@lisasoft.com> writes:
> My question is in a rollback scenario is it possible to get PSQL to return
> a non 0 exit status?

Maybe you could use -c instead of -f?

$ psql -c 'select 1; select 1/0' regression
ERROR:  division by zero
$ echo $?
1

You won't need explicit BEGIN/END because this is already a single
transaction.
        regards, tom lane



Re: PSQL return coder

From
Merlin Moncure
Date:
On Thu, Oct 10, 2013 at 1:52 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> James Sewell <james.sewell@lisasoft.com> writes:
>> My question is in a rollback scenario is it possible to get PSQL to return
>> a non 0 exit status?
>
> Maybe you could use -c instead of -f?
>
> $ psql -c 'select 1; select 1/0' regression
> ERROR:  division by zero
> $ echo $?
> 1
>
> You won't need explicit BEGIN/END because this is already a single
> transaction.

According to the man page,
"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,
and3 if an      error occurred in a script and the variable ON_ERROR_STOP was set."
 

So for a longer script ON_ERROR_STOP might be the ticket (which is
usually a good idea anyways).

merlin



Re: PSQL return coder

From
James Sewell
Date:
I was avoiding ON_ERROR_STOP because I was using ON_ERROR_ROLLBACK, but have just realised that if I encase my SQL in a transaction then rollback will still happen.

Perfect!


James Sewell,
PostgreSQL Team Lead / Solutions Architect
______________________________________
 

Level 2, 50 Queen St, Melbourne VIC 3000

(+61) 3 8370 8000  W www.lisasoft.com  (+61) 3 8370 8099
 


On Fri, Oct 11, 2013 at 12:25 AM, Merlin Moncure <mmoncure@gmail.com> wrote:
On Thu, Oct 10, 2013 at 1:52 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> James Sewell <james.sewell@lisasoft.com> writes:
>> My question is in a rollback scenario is it possible to get PSQL to return
>> a non 0 exit status?
>
> Maybe you could use -c instead of -f?
>
> $ psql -c 'select 1; select 1/0' regression
> ERROR:  division by zero
> $ echo $?
> 1
>
> You won't need explicit BEGIN/END because this is already a single
> transaction.

According to the man page,
"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."

So for a longer script ON_ERROR_STOP might be the ticket (which is
usually a good idea anyways).

merlin



The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.