我正在使用psql命令连接postgreSQL数据库并发出查询.有人能告诉我如何在shell脚本中判断已执行查询的返回状态吗.

我使用了echo $?命令来判断状态,但它总是返回零.

谢谢你的帮助.

推荐答案

psql返回代码is documented as:

退出状态

你可能只是想用ON_ERROR_STOP.

Failure getting tested and reported to the shell:

$ psql -d test -v "ON_ERROR_STOP=1" <<EOF
select error;
select 'OK';
EOF

ERROR:  column "error" does not exist
LINE 1: select error;

$ echo $?
3

Failure getting ignored and not reported to the shell:

$ psql -d test  <<EOF
select error;
select 'OK';
EOF
ERROR:  column "error" does not exist
LINE 1: select error;
               ^
 ?column? 
----------
 OK
(1 row)

$ echo $?
0

Postgresql相关问答推荐

利用PostgreSQL查询中表的顺序来计算包含每次时间的时间范围

PostgreSQL按描述请求

使用 pgx 扫描范围类型

是否可以在 postgres jsonb 列中的列表上创建索引

PostgreSQL:如何从另一列的 json 值替换为一列?

我在try 访问我的数据库表时在 postgresql 中收到 aclcheck_error 错误

Postgres >= 和 <= 具有特殊字符的行为

有对(type_id,element_id),如(1,1),(1,2),..(5,3).如果我需要获取没有 element_id 1 的类型 ID,如何从结果中排除 type_id?

Nodejs应用程序的node-postgres vs pg-promise

在psql中,为什么有些命令没有效果?

brew 程序更新恼人的错误信息

SQLAlchemy、Psycopg2 和 Postgresql 复制

将 SELECT 结果作为参数传递给 postgreSQL 函数

Postgres 删除表语法错误

在 postgreSQL 中更改序列名称

如何使用 psql 命令列出、创建、使用和判断数据库?

我怎样才能知道 Postgresql 表空间中有什么?

如何在 postgres 中使用 json_populate_recordset 解析 json

大型查询后 psycopg2 泄漏内存

在 postgres 中存在不同的 string_agg 问题