PostgreSQLメモ : 表形式データ向けpspgページャを試してみる
pspg
psql
使用時にはページャとしてless
を使っているが、pspg
という表形式データ向けのページャがあったので試してみる。READMEによるとmysql
クライアントでも使えるとのこと。
インストール
試した環境はCent OS 7, PostgreSQL 9.6だったのでPostgreSQL RPM Building Project - Yum Repositoryのリンクを辿っていって、
RepoView: PostgreSQL PGDG 9.6 Updates RPMs
にあるpspg-0.5-1.rhel7.x86_64.rpmをダウンロード。
そして、下記コマンドでインストールする。
$ sudo yum localinstall pspg-0.5-1.rhel7.x86_64.rpm
インストールが成功していればpspg
コマンドが使えるようになっている。
$ pspg --version pspg-0.5 $ pspg --help pspg is a Unix pager optimized for table browsing. Usage: pspg [OPTION] Options: -b black-white style -s N set color style number (1..14) -c N fix N columns (1..4) -f file open file -X don't use alternate screen --help show this help --no-mouse don't use own mouse handling --no-sound don't use beep when scroll is not possible -F, --quit-if-one-screen quit if content is one screen -V, --version show version pspg shares lot of key commands with less pager or vi editor.
使い方
PAGER
としてpspg
を指定すれば使えるようだ(常に設定しておきたいのであれば~/.psqlrc
などに記述する)。
Vimのようにh/j/k/l
で移動でき、ヘッダ行が固定されるのがよい。
$ psql -U postgres -p 5432 -d dvdrental psql (9.6.0) Type "help" for help. dvdrental=# \setenv PAGER 'pspg' dvdrental=# \pset pager always dvdrental=# select * from film;
0
, 1
, 2
などの数字を入力すると表示列が固定できるので横に長いデータでも閲覧しやすい(図ではfilm_id
が固定されている。0
で解除、1
で先頭列のfilm_id
を固定、2
で2列目のtitle
まで固定する)。
pspg
で使用できるキーボードショートカット一覧は以下の通り。
キー | 操作内容 |
---|---|
0, 1, 2, 3, 4 | freeze first N columns |
KEY_UP, k | navigate backward by one line |
KEY_DOWN, j | navigate forward by one line |
KEY_LEFT, h | scroll to left |
KEY_RIGHT, l | scroll to right |
Ctrl Home, g | go to the start of file |
Ctrl End, G | go to the end of file |
H | go to first line of current window |
M | go to half of current window |
L | go to end of current window |
PPAGE, Ctrl B | backward one window |
NPAGE, Ctrl F, space | forward one window |
HOME, ^ | go to begin of line, first column |
END, $ | go to end of line, last column |
Ctrl E | scroll a window down |
Ctrl Y | scroll a window up |
Ctrl D | forward a half window |
Ctrl U | backward a half window |
s | save content to file |
/ | search for a pattern which will take you to the next occurrence |
? | search for a pattern which will take you to the previous occurrence |
n | for next match in backward direction |
N | for previous match in forward direction |
Alt M | switch (on, off) own mouse handler |
Mouse button wheel | scroll vertical |
Alt Mouse button wheel | scroll horizontal |
q, F10, Esc Esc, Esc 0 | quit |
色設定
色の設定を変更する場合は-s N
で指定する(Nは0〜14の数字)。
dvdrental=# \setenv PAGER 'pspg -s 0'
dvdrental=# \setenv PAGER 'pspg -s 11'