如何正确观看psql命令?

问题描述 投票:0回答:2

种子

我为我的数据库播种

curl -s http://site/api/seed/user/1000

测试

我做到了

psql --username=nm -d portal -h 192.168.1.27 -c "SELECT count(*) from users;"

我得到了

 count                                                                                                                                                                                 
-------                                                                                                                                                                                
  1000                                                                                                                                                                                 
(1 row)

观看

watch psql --username=nm -d portal -h 192.168.1.27 -c "SELECT count(*) from users;"

我得到了

Every 2.0s: psql --username=nm -d portal -h 192.168.1.27 -...  bh-macbook-pro-15-512gb.local: Tue Nov 21 15:19:08 2017

sh: -c: line 0: syntax error near unexpected token `('                                                                         
sh: -c: line 0: `psql --username=nm -d portal -h 192.168.1.27 -c SELECT count(*) from users;' 

如何修复此错误?

linux macos unix psql watch
2个回答
9
投票

将命令作为单个带引号的参数提供:

watch 'psql --username=nm -d portal -h 192.168.1.27 -c "SELECT count(*) from users;"'

0
投票

如果您想使用
\watch
命令

终端一行

要在终端中的 一个命令中运行所有内容,请在 SQL 命令后添加

\watch,然后 使用管道 
|
来提供命令。 参数
-c
不起作用(
-c
必须以分号
;
结尾,因此命令
\watch;
无法识别)。

echo '[your_sql_cmd]; \watch' | psql [your_connection]

示例:

echo 'SELECT count(*) from users; \watch' | psql --username=nm -d portal -h 192.168.1.27

互动终端

要直接在psqlPostgreSQL交互终端上执行命令,连接到数据库后,执行命令,然后使用

\watch
命令。示例:

SELECT count(*) from users;
\watch

\watch
命令说明:

\watch [ i[nterval]=seconds ] [ c[ount]=times ] [ seconds ]

重复执行当前查询缓冲区(如 \g 那样)直到中断,或者查询失败,或者达到执行计数限制(如果给定)。在两次执行之间等待指定的秒数(默认为 2)。为了向后兼容,可以使用或不使用interval=前缀来指定秒。每个查询结果都显示一个标题,其中包括 \pset 标题字符串(如果有)、查询开始的时间以及延迟间隔。

来源:https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-META-COMMAND-WATCH

© www.soinside.com 2019 - 2024. All rights reserved.