Writing a psql script that exits on the first error
The default mode for the psql
 script tool is to continue processing when it finds an error. This sounds silly, but it exists for historical compatibility only. There are some easy and permanent ways to avoid this, so let's look at them.
Getting ready
Let's start with a simple script, with a command we know will fail:
$ $EDITOR test.sql mistake1; mistake2; mistake3;
Execute the following script using psql
 to see what the results look like:
$ psql -f test.sql psql:test.sql:1: ERROR:Â Â syntax error at or near "mistake1" LINE 1: mistake1; Â Â Â Â Â Â Â Â ^ psql:test.sql:2: ERROR:Â Â syntax error at or near "mistake2" LINE 1: mistake2; Â Â Â Â Â Â Â Â ^ psql:test.sql:3: ERROR:Â Â syntax error at or near "mistake3" LINE 1: mistake3; Â Â Â Â Â Â ...