Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Huh? Report a bug against psql maybe? – alex Jul 17 '11 at 10:09
    
It can be a reasonably intentional behavior. – Adam Matan Jul 17 '11 at 14:34

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

\set ON_ERROR_STOP 1
share|improve this answer
    
Ugh Postgres... That's annoying. – Jmoney38 Dec 23 '14 at 13:05
    
another way: cat myfile.sql | psql -v ON_ERROR_STOP=1 . This is nice because it isn't necessary to modify the SQL file, the checking can be done only via the command line. – johntellsall Aug 11 '15 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.