HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

PostgreSQL exit status when running a file

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlfilestatusexitrunningwhen

Problem

When running PostgreSQL with a single SQL command error codes are returned as expected:

% psql -c "SELECT * FROM AWDASDASDASDAS" my_db
ERROR:  relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
1


But when running a file, the error is suppressed:

% psql -f test.sql my_db
psql:test.sql:1: ERROR:  relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
0


Any idea how to get these errors back?

Solution

Adding this to the first line of the SQL file does the trick:

\set ON_ERROR_STOP 1


New comments (Thanks!) suggest the following solutions:

-v ON_ERROR_STOP=1
-v ON_ERROR_STOP=on

Code Snippets

\set ON_ERROR_STOP 1
-v ON_ERROR_STOP=1
-v ON_ERROR_STOP=on

Context

StackExchange Database Administrators Q#3904, answer score: 13

Revisions (0)

No revisions yet.