运行文件时PostgreSQL退出状态


16

使用单个SQL命令运行PostgreSQL时,按预期返回错误代码:

% psql -c "SELECT * FROM AWDASDASDASDAS" my_db
ERROR:  relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
1

但是在运行文件时,该错误被抑制:

% psql -f test.sql my_db
psql:test.sql:1: ERROR:  relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
0

任何想法如何找回这些错误?

Answers:


12

将其添加到SQL文件的第一行即可达到目的:

\set ON_ERROR_STOP 1

新评论(谢谢!)提出以下解决方案:

-v ON_ERROR_STOP=1
-v ON_ERROR_STOP=on

2
@ pt12lol对我也失败了,这确实起作用了:psql -v ON_ERROR_STOP=on -f file_name.sql
Morgen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.