Bash 如何检查.sql脚本执行是否成功?

Bash 如何检查.sql脚本执行是否成功?,bash,postgresql,psql,Bash,Postgresql,Psql,我正在使用以下命令加载导出的数据库: psql -c 'drop database database1' psql -c 'create database database1' psql database1 < script.sql 这总是输出OK。退出代码始终为0,即使script.sql完成时出现错误: psql database1 < script.sql ERROR: constraint "test_id" for relation "test" already exis

我正在使用以下命令加载导出的数据库:

psql -c 'drop database database1'
psql -c 'create database database1'
psql database1 < script.sql
这总是输出OK。退出代码始终为0,即使script.sql完成时出现错误:

psql database1 < script.sql
ERROR: constraint "test_id" for relation "test" already exists
echo $?
0

如何确认sql导入成功?

您可以检查命令是否引发如下错误:

psql database1 < script.sql || echo 'error occurred'
双管道| |表示管道的左侧部分,如果错误,则表示管道的右侧部分


希望有帮助

这个线程可能会有帮助:简言之,可能重复:改为尝试:psql-v ON\u ERROR\u STOP=1 database1psql database1 < script.sql || echo 'error occurred'