How can I stop a Postgres script when it encounters an error?

前端 未结 4 798
眼角桃花
眼角桃花 2020-12-13 11:34

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.

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 12:24

    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
    

提交回复
热议问题