Unix pager (with very rich functionality) designed for work with tables. Designed for PostgreSQL, but MySQL is supported too. Works well with pgcli too. Can be used as CSV or TSV viewer too. It supports searching, selecting rows, columns, or block and export selected area to clipboard.
Everybody who uses
psqluses
lesspager. It is working well, but there is not any special support for tabular data. I found few projects, but no one was completed for this purpose. I decided to write some small specialized pager for usage as
psqlpager.
This pager can be used from the following clients command line clients too:
mysql
sqlite
pgcli
monetdb
Trino (formerly Presto SQL)
mcviewor
FoxPro- http://okbob.blogspot.com/2019/12/pspg-themes-what-you-use-it.html
-amenu will use ascii borders
-bblack/white theme
-Xdoesn't clean screen on the end
-s Nuse theme (default theme is mc theme)
-c Nfreeze first N columns
-f fileopen file (default stdin)
--no-watch-filedon't watch changes of file
--force-uniborderreplace ascii border by unicode borders
-g --hilite-searchdon't highlight lines for searches
-G --HILITE-SEARCHdon't highlight lines for searches ever
--helpshow this help
-i --ignore-caseignore case in searches that do not contain uppercase
-I --IGNORE-CASEignore case in all searches
--less-status-barstatus bar like less pager
--line-numbersshow line number column
--no-mousewithout own mouse handling (cannot be changed in app)
--no-soundwithout sound effect
-F,
--quit-if-one-screenquit if content is one screen
-V,
--versionshow version
--aboutshow info about authors
--csvinput format is csv
--tsvinput format is tsv
--double-headerheader line is doubled
--borderborder used for formatted csv
--csv-separatorspecial char used as separator inside csv documents
--null striingnull string (default "")
--ninot interactive mode (format csv to table and quit)
--no-cursorthe line cursor will be hidden
--no-commandbarthe bottom bar will be hidden
--no-topbarthe top bar will be hidden
--no-barsboth bars will be hidden
--no-scrollbarscrollbar will be hidden
--no-sigint-search-resetsigint is not used to reset searching
--no-xterm-mouse-modedon't use extended mouse protocol
--tabular-cursorcursor is displayed only for table
--only-for-tablesuse std pager when content is not a table
--bold-labelsbold font for row, column labels
--bold-cursorbold font for cursor
--vertical-cursorshow column cursor
--on-sigint-exitdouble escape or ctrl c ending pager
--quit-on-f3exit on press F3 (like mc viewer)
--skip-colums-likespace separated list of ignored columns (only for csv and tsv format)
-q,
--queryexecute query
-w,
--watch nrepeat query execution every time sec
-d,
--dbnamedatabase name
-h,
--hostdatabase host name
-p,
--portdatabase port
-U,
--usernamedatabase user name
-W,
--passwordforce password prompt
--pgcli-fixused for fixing some pgcli related issues
--clipboard-app=[1,2,3]specify clipboard application (1 wl-clipboard, 2 xclip, 3 pbcopy)
--no-sleepdisable waits used for reduction of terminal flickering
Options can be passed inside env variable
PSPGtoo.
see http://okbob.blogspot.cz/2017/07/i-hope-so-every-who-uses-psql-uses-less.html
The pager can be ended by pressing keys q or F10 or Esc 0. With option
--on-sigint-exitthen the pager is closed by pressing keys Ctrl+c or Esc Esc.
Column search is case insensitive every time. Searched column is marked by vertical cursor. Last non empty string searching pattern is used when current searching pattern is empty string. Searching is starting after visible vertical column or on first visible not freezed columns (after some horizontal scrolling) or on first column. After last column searching starts from first again.
For clipboard support the clipboard application should be installed: 1. wl-clipboard (Wayland), 2. xlip (xwindows) or 3. pbcopy (MacOS).
pspgtry to translate unicode symbol '∅' to NULL every time. If you don't use special setting by
\pset null ..., then
psqldisplays empty string instead NULL.
pspghasn't any special detection (in export routines) for this case. You should to check and enable or disable menu item
Empty string is NULL.
pspghas automatic detection of clipboard application. Unfortunatelly, this detection should not to work for same cases. You can specify the application by specify number (1,2,3) to
--clipboard-appoption.
V: [d/d d..d]- vertical cursor: (column number)/(columns) (char possitions from) .. (char possitions to)
FC: d- freezed columns length in chars
C: d..d/d- unfreezed visible data in chars (from .. to)/(total)
L:[d + d d/d]- lines (number of first visible line) + (number of line of display), (current line)/(lines)
d%- percent of already displayed data
It works well with miller http://johnkerl.org/miller/doc/index.html
mlr --icsv --opprint --barred put '' obce.csv | pspg --force-uniborder
New version has integrated csv support - just use
--csvoption.
It can be integrated into mc
/etc/mc/mc.extto your
~/.config/mc directory
#csv regex/\.csv View=pspg -f %f --csv
mc
pspgon Cygwin, then some temporary freezing of scrolling was reported In this case, please, use an option
--no-sleep.
The result of query can be refreshed every n seconds.
pspgremembers cursor row, possible vertical cursor, possible ordering. The refreshing should be paused by pressing space key. Repeated pressing of this key enables refreshing again.
pspguses inotify API when it is available, and when input file is changed, then
pspgreread file immediately. This behave can be disabled by option
--no-watch-fileor by specification watch time by option
--watch.
pspgcan continually read from file or FIFO (named pipe). This behave should should be forced by option
--stream.
When last writer to FIFO is ending, then
pspgis ending too. This behave can be changed by option
--hold-stream. Default value is 0. When
--hold-stream=1then
pspgwill try to reopen FIFO and will hang until the FIFO will be opened for writing.
--hold-stream=2is different strategy. The
pspgreopen FIFO in write mode, and then FIFO will be opened until
pspgis running.
pspgcan read a stream of tabular data from pipe or from file (with an option
--streamor it can read a stream of queries from pipe or from file (with an option
--querystream). It is good to use these option with option
--hold-stream.
Without this option, there are possible unwanted issues when pipe will be closed.
The query stream mode is an sequence of SQL statements separated by char GS (Group separator - 0x1D on separated line.
you should to add to your profile:
for Postgres 10 and older
export PAGER="pspg"
for postgres 11 and newer
export PSQL_PAGER="pspg"
or "\setenv PAGER pspg" to .psqlrc
and .psqlrc
\set QUIET 1 \pset linestyle unicode \pset border 2 \pset null ∅ \unset QUIET
some possible configuration:
-- Switch pagers with :x and :xx commands \set x '\setenv PAGER less' \set xx '\setenv PAGER \'pspg -bX --no-mouse\'' :xx
LC_CTYPEshould be correct. Mainly when you use unicode borders. ncurses doesn't display unicode borders (produced by
psql) without correct setting of this variable. Is possible to check a value 'C.UTF8'.
When you use a option
--only-for-tables, then
PAGERto
pspgand
PSQL_PAGERto
lessor
PAGERto
lessand
PSQL_PAGERto
pspg
MariaDB [sakila]> pager pspg -s 14 -X --force-uniborder --quit-if-one-screen PAGER set to 'pspg -s 14 -X --force-uniborder --quit-if-one-screen' MariaDB [sakila]> select now(); MariaDB [sakila]> select * from nicer_but_slower_film_list limit 100;
SQLite native client doesn't produce well formatted output, but can be forced to generate CSV format - and this format is well readable for
pspg
sqlite3 -csv -header testdb.db 'select * from foo2' | pspg --csv --csv-header=on --double-header
pgcli needs the following configuration options (
~/.config/pgcli/config):
pager = /usr/bin/pspg --csv --rr=2 --quit-if-one-screen --ignore-case --csv-header on --pgcli-fix table_format = csv
pspg try to use xterm mouse mode 1002, when terminal and ncurses are not too antique. If there are problems with usage - unwanted visual artefacts when you move with mouse when some mouse button is pressed, then 1. please, report issue (please, attach log file), 2. use an option
--no-xterm-mouse-modeand
pspgwill not try to activate this mode.
Some linker issues can be fixed by:
I changed gcc -lncursesw pager.c -o pspg -ggdb to gcc pager.c -o pspg -ggdb -lncursesw
On some old systems a compilation fails with error
/home/user/Src/pspg-0.6/src/pspg.c:2403: undefined reference toIn this case comment line with function set_escdelayset_escdelay
If you want to use
pspgas Postgres client, then you need run
configure --with-postgres=yes. On Fedora with own Postgres build I had to install
openssl-develpackage and I had to set
export PKG_CONFIG_PATH="/usr/local/pgsql/master/lib/pkgconfig/".
When you compile code from source, run ./configure first. Sometimes ./autogen.sh first
If you would to display UTF-8 characters, then
pspgshould be linked with
ncurseswlibrary. UTF-8 characters are displayed badly when library
ncurseswis used. You can see broken characters with incorrect locale setting too.
You can check wide chars support by
pspg --version. Row
ncurses with wide char supportis expected. Re-run
configurewith
--with-ncurseswoption. When this command fails check if development package for ncuresesw library is installed.
# brew install pspg
# apt-cache search pspg # apt-get install pspg
# dnf install pspg
The pspg is available from community repository https://yum.postgresql.org/packages.php
# apk add pspg
# emerge -av dev-db/pspg
The Arch User Repository contains two versions:
Use the AUR helper of your choice or git and
makepkgto install pspg.
# pkg install pspg
# port install pspg
There are few issues requires manual code changes for successful compilation - we successfully tested
pspg, but although
pspgwas linked with ncursesw libraries, the utf8 encoding support didn't work fully correctly - probably due some issues in
libclibrary. There are problems with chars encoded to 3bytes - unicode borders, .. Two bytes unicode chars should be displayed well.
You can use
pspgwith usual accented chars, but unicode bordes should not be used. Replacement ascii borders by special borders chars (by ncurses technology) works well - looks on
Options|Force unicode bordersoption.
Solaris
makedoesn't support conditional statements - should be removed So, remove unsupported functionality from
Makefile(
ifdef,
endif), replace
-includeby
includefirst.
After running
configureremove link on
termcaplibrary from
config.make. It is garabage produced by
readlineautomake script. Combination with
ncurseslibraries makes some linking issues.
export CURSES_CFLAGS="-I/usr/include/ncurses/" export PANEL_LIBS="-lpanelw" ./configure
export CFLAGS="-m64 -I/opt/csw/include" export LDFLAGS="-L/opt/csw/lib/64 -R/opt/csw/lib/64" export PKG_CONFIG_PATH="/opt/csw/lib/64/pkgconfig" ./configure
This project uses st_menu library - implementation of CUA menubar and pulldown menu for ncurses https://github.com/okbob/ncurses-st-menu
If you like it, send a postcard from your home country to my address, please:
Pavel Stehule Skalice 12 256 01 Benesov u Prahy Czech Republic
I invite any questions, comments, bug reports, patches on mail address [email protected]