patternsqlModerate
PostgreSQL exit status when running a file
Viewed 0 times
postgresqlfilestatusexitrunningwhen
Problem
When running PostgreSQL with a single SQL command error codes are returned as expected:
But when running a file, the error is suppressed:
Any idea how to get these errors back?
% psql -c "SELECT * FROM AWDASDASDASDAS" my_db
ERROR: relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
1But 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 $?
0Any idea how to get these errors back?
Solution
Adding this to the first line of the SQL file does the trick:
New comments (Thanks!) suggest the following solutions:
\set ON_ERROR_STOP 1New comments (Thanks!) suggest the following solutions:
-v ON_ERROR_STOP=1
-v ON_ERROR_STOP=onCode Snippets
\set ON_ERROR_STOP 1-v ON_ERROR_STOP=1
-v ON_ERROR_STOP=onContext
StackExchange Database Administrators Q#3904, answer score: 13
Revisions (0)
No revisions yet.