O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing a psql script that exits on first error

The default mode for the psql script tool is to continue processing when it finds an error. That sounds dumb, though it exists for historical compatibility only. There are some easy and mostly permanent ways around that, so let's look at them.

Getting ready

Let's start with a trivial script, with a command we know will fail as follows:

$ $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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required