156

Is there a way to specify that when executing a sql script it stops when encountering the first error on the script, it usually continues, regardless of previous errors.

1
  • If you run it from the terminal you can do psql -v ON_ERROR_STOP=on [FLAGS] -f file_name Commented Mar 27, 2024 at 20:46

5 Answers 5

237

I think the solution to add following to .psqlrc is far from perfection

\set ON_ERROR_STOP on

there exists much more simple and convenient way - use psql with parameter:

psql -v ON_ERROR_STOP=1

better to use also -X parameter turning off .psqlrc file usage. Works perfectly for me

p.s. the solution found in great post from Peter Eisentraut. Thank you, Peter! http://petereisentraut.blogspot.com/2010/03/running-sql-scripts-with-psql.html

Sign up to request clarification or add additional context in comments.

3 Comments

-v ON_ERROR_STOP=ON also works, at least with 9.2. I suspect any of the variants of boolean "true" are allowed.
It doesn't work in interactive mode, which confused me for a minute.
Without "on" explicitly set, it will toggle. This bit me when I had a script working great and then broke it into several scripts that all ran the same initialization script. Then, the subscripts were toggling this parameter on and off. :(
36

I assume you are using psql, this might be handy to add to your ~/.psqlrc file.

\set ON_ERROR_STOP on

This will make it abort on the first error. If you don't have it, even with a transaction it will keep executing your script but fail on everything until the end of your script.

And you probably want to use a transaction as Paul said. Which also can be done with psql --single-transaction ... if you don't want to alter the script.

So a complete example, with ON_ERROR_STOP in your .psqlrc:

psql --single-transaction --file /your/script.sql

2 Comments

Even if the transaction fails, the exit status of the psql command is still 0.
Indeed, even if --single-transaction is used, -v ON_ERROR_STOP=1 is still necessary for a non zero exist status
9

It's not exactly what you want, but if you start your script with begin transaction; and end with end transaction;, it will actually skip everything after the first error, and then it will rollback everything it did before the error.

2 Comments

True, but, it still parses everything. And if you want to do a second transaction only if the first succeeded, this won't work.
Yes, and not to forget continuing when it encounters DDL Create table errrors... (version: postrgres 10). Yes, it skips one table and goes onto the others...
3

I always like to reference the manual directly.

From the PostgreSQL Manual:

Exit Status

psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g. out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set.

By default if the sql code you are running on the PostgreSQL server error psql won't quit an error. It will catch the error and continue. If, as mentioned above, you set the ON_ERROR_STOP setting to on, when psql catches an error in the sql code it will exit and return 3 to the shell.

Comments

2

For example, you can enable ON_ERROR_STOP which is off by default when you log in with the user john and the database apple as shown below. *ON_ERROR_STOP is off automatically after logout:

psql -U john -v ON_ERROR_STOP=on apple
psql -U john --variable=ON_ERROR_STOP=on apple
psql -U john --set=ON_ERROR_STOP=on apple 

And, you can enable ON_ERROR_STOP when you run a script from the file test.sql as shown below:

psql -U john -v ON_ERROR_STOP=on -f test.sql apple

And, you can enable ON_ERROR_STOP after login as shown below:

\set ON_ERROR_STOP on

Or. *This command can only enable ON_ERROR_STOP without toggling on and off:

\set ON_ERROR_STOP

And, you can show the value of ON_ERROR_STOP with \echo as shown below. *Don't forget to put : just before ON_ERROR_STOP:

postgres=# \echo :ON_ERROR_STOP
on

*Memos:

  • Don't use the lowercase on_error_stop which doesn't work.

  • You can enable ON_ERROR_STOP with oN, 1, TrUe, tR, etc.

  • You can disable ON_ERROR_STOP with oFf, 0, FaLsE, fA, etc.

  • It happens that error doesn't stop execution when you run a script from a file.

  • RAISE statement with DEBUG, LOG, INFO, NOTICE or WARNING cannot stop execution even if ON_ERROR_STOP is on while RAISE statement with EXCEPTION or nothing can. *My question explains it in detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.