如何在遇到错误时停止 Postgres 脚本?
有没有办法指定在执行 sql 脚本时遇到脚本上的第一个错误时停止,通常会继续,而不管以前的错误如何。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为向 .psqlrc 添加以下内容的解决方案远非完美,
存在更简单和方便的方法 - 使用带有参数的 psql:
最好还使用
-X
参数关闭 .psqlrc 文件使用。非常适合我
。在 Peter Eisentraut 的精彩帖子中找到了解决方案。谢谢你,彼得!
http://petereisentraut.blogspot.com/2010/03/running-sql-脚本-with-psql.html
I think the solution to add following to .psqlrc is far from perfection
there exists much more simple and convenient way - use psql with parameter:
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
我假设您正在使用
psql
,将其添加到您的~/.psqlrc
文件中可能会很方便。这将使其在出现第一个错误时中止。如果您没有它,即使有事务,它也会继续执行您的脚本,但一切都会失败,直到脚本结束。
正如保罗所说,你可能想使用交易。如果您不想更改脚本,也可以使用 psql --single-transaction ... 来完成。
这是一个完整的示例,在 .psqlrc 中包含 ON_ERROR_STOP:
I assume you are using
psql
, this might be handy to add to your~/.psqlrc
file.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:
这并不完全是您想要的,但是如果您以
begin transaction;
开始脚本并以end transaction;
结束,它实际上会跳过第一个错误之后的所有内容,然后它将回滚错误之前所做的一切。It's not exactly what you want, but if you start your script with
begin transaction;
and end withend transaction;
, it will actually skip everything after the first error, and then it will rollback everything it did before the error.我总是喜欢直接参考手册。
来自 PostgreSQL 手册:
默认情况下,如果您在 PostgreSQL 服务器上运行的 sql 代码出现错误,psql 不会退出错误。它将捕获错误并继续。如果如上所述,将
ON_ERROR_STOP
设置设置为 on,则当 psql 捕获 sql 代码中的错误时,它将退出并向 shell 返回3
。I always like to reference the manual directly.
From the PostgreSQL Manual:
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 return3
to the shell.例如,您可以启用 当您使用用户
john
和数据库apple
登录时,ON_ERROR_STOP 默认为off
,如下所示。 *ON_ERROR_STOP
在注销后自动关闭
:并且,当您从文件
test.sql< 运行脚本时,您可以启用
ON_ERROR_STOP
/code> 如下所示:并且,您可以在登录后启用
ON_ERROR_STOP
如下所示:或者。 *此命令只能启用
ON_ERROR_STOP
,而无需切换on
和off
:并且,您可以显示
ON_ERROR_STOP
的值与 \echo 为如下所示。 *不要忘记在ON_ERROR_STOP
之前添加:
:*备注:
不要使用小写的
on_error_stop
,它不会工作。您可以使用
oN
、1
、TrUe
、tR
启用ON_ERROR_STOP
等您可以使用
oFf
、0
、FaLsE
、fA
禁用ON_ERROR_STOP
等当您从文件运行脚本时,错误不会停止执行。
带有
DEBUG 的 RAISE 语句
、
LOG
、INFO
、NOTICE
或WARNING
即使ON_ERROR_STOP< 也无法停止执行/code> 是
on
,而RAISE
语句带有EXCEPTION
或什么都不能。 *我的问题对此进行了解释详细信息。For example, you can enable ON_ERROR_STOP which is
off
by default when you log in with the userjohn
and the databaseapple
as shown below. *ON_ERROR_STOP
isoff
automatically after logout:And, you can enable
ON_ERROR_STOP
when you run a script from the filetest.sql
as shown below:And, you can enable
ON_ERROR_STOP
after login as shown below:Or. *This command can only enable
ON_ERROR_STOP
without togglingon
andoff
:And, you can show the value of
ON_ERROR_STOP
with \echo as shown below. *Don't forget to put:
just beforeON_ERROR_STOP
:*Memos:
Don't use the lowercase
on_error_stop
which doesn't work.You can enable
ON_ERROR_STOP
withoN
,1
,TrUe
,tR
, etc.You can disable
ON_ERROR_STOP
withoFf
,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
orWARNING
cannot stop execution even ifON_ERROR_STOP
ison
whileRAISE
statement withEXCEPTION
or nothing can. *My question explains it in detail.