\watch
命令很棒。
它附加一个查询的输出,一遍又一遍地观察。
例如,当我之前运行查询SELECT id, nickname FROM users;
并输入\watch
时,如果没有新数据,我会一遍又一遍地获得相同的输出:< / p>
my_db=# SELECT id, nickname FROM users;
id | nickname
----+----------
1 | AntonAL
(1 row)
my_db=# \watch
четверг, 31 августа 2017 г. 13:23:26 (every 2s)
id | nickname
----+----------
1 | AntonAL
(1 row)
четверг, 31 августа 2017 г. 13:23:28 (every 2s)
id | nickname
----+----------
1 | AntonAL
(1 row)
在观看执行之间有没有选项来清除屏幕输出?
我希望获得类似仪表板的体验,当显示和刷新一组行时,不会反复追加到Postgres控制台输出。
答案 0 :(得分:2)
我遇到了这个问题,并且在#postgresql
上有MatheusOl的建议,以下内容对我有用:
\C `clear`
SELECT * FROM pg_tables \watch 10
\C [value]
命令序列允许您设置标题,并且可以设置(使用反引号括起来)clear
返回的ANSI转义。
答案 1 :(得分:0)
我不认为psql
有任何方法可以清除终端。也许用bash做什么?
while true;
clear;
psql -c "SELECT id, nickname FROM users"
sleep 2;
done
答案 2 :(得分:0)
类似于最后一个答案,但是使用“ watch”命令(在Linux上)稍微简单一些:
watch 'psql -c "SELECT id, nickname FROM users"'
...这将每2秒(默认间隔)用psql执行一次查询,您还可以向此命令添加不同的参数:
$ watch --help
Usage:
watch [options] command
Options:
-b, --beep beep if command has a non-zero exit
-c, --color interpret ANSI color and style sequences
-d, --differences[=<permanent>]
highlight changes between updates
-e, --errexit exit if command has a non-zero exit
-g, --chgexit exit when output from command changes
-n, --interval <secs> seconds to wait between updates
-p, --precise attempt run command in precise intervals
-t, --no-title turn off header
-x, --exec pass command to exec instead of "sh -c"
-h, --help display this help and exit
-v, --version output version information and exit
For more details see watch(1).
我喜欢用它来监视pg_stat_activity的会话活动:
$ cat sessions.sql
select pid as process_id,
usename as username,
datname as database_name,
client_addr as client_address,
application_name,
backend_start,
state,
state_change
from pg_stat_activity;
$ watch -d psql -f sessions.sql
...,我们每2秒刷新一次输出:
Every 2.0s: psql -f sessions.sql Sun Sep 29 17:38:20 2019
process_id | username | database_name | client_address | application_name | backend_start | state | state_change
------------+----------+---------------+----------------+------------------+-------------------------------+--------+-------------------------------
1240 | postgres | | | | 2019-09-29 15:40:36.697364-04 | |
1236 | | | | | 2019-09-29 15:40:36.697681-04 | |
14025 | postgres | postgres | | psql | 2019-09-29 17:38:20.508996-04 | active | 2019-09-29 17:38:20.511069-04
1233 | | | | | 2019-09-29 15:40:36.69497-04 | |
1232 | | | | | 2019-09-29 15:40:36.695851-04 | |
1234 | | | | | 2019-09-29 15:40:36.690183-04 | |
(6 rows)