Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I am writing a shell script which makes calls to psql using 2 forms... one is by command (-c), the other is by file (-f).

e.g. psql -c "create table foo (bar integer)"

psql -f foobar.sql

One difference between these forms is that a call by command (-c) returns a non-zero exit code if an error is encountered, while a call by file (-f) always seems to return zero.

I'm wondering if there is a workaround for this behaviour? (i.e. return non-zero if an error occurs while executing a file).


share|improve this question

You can use below statement.

psql -v ON_ERROR_STOP=1 -f foobar.sql

This will return the correct return code even foobar.sql file doesn't enable ON_ERROR_STOP at the top of the file.

share|improve this answer
up vote 8 down vote accepted

I found out how to resolve this.

I need to enable ON_ERROR_STOP at the top of the file.


\set ON_ERROR_STOP true
share|improve this answer
+1 for looking at the manpage :) – Server Horror Jun 9 '09 at 18:54
+1 for the best solution because it allows me to set the variable in the script, so I can trust it always runs correctly and don't have to worry about runtime context. – Brandon Dec 27 '14 at 4:48

Your Answer


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.