在MariaDB Xpand中使用PostgreSQL分页器

I’m not an anti-GUI person. In fact, I wrote three books about web GUI development with Java. However, I also like the command-line interface (CLI), especially text-based UIs. After a year of exploring MariaDB and the DevOps world, I got to discover and play with many text-based CLI tools that I didn’t know even existed. These tools are especially useful when connecting to remote servers that don’t have a GUI.

我常用的一个特殊CLI工具是mariadb SQL客户端(在MySQL领域中称为mysql)——这是一个用于连接到兼容MariaDB数据库的CLI程序。通过它,你可以向数据库服务器发送SQL查询和其他命令。

基于CLI的MariaDB SQL客户端

mariadb SQL客户端提供了多种配置选项,其中之一是能够设置终端分页器。如果你熟悉Linux,可能已经听说过或使用过moreless分页器。你可以通过环境变量PAGER来设置分页器,mariadb会自动采用它。或者,你也可以仅在当前会话中使用mariadb提示符来设置分页器。例如,连接到数据库后,运行以下命令以使用less分页器:

MariaDB SQL

 

pager less

下次运行SQL查询时,你将能够使用键盘上的箭头键浏览结果集。

使用mariadb SQL客户端设置分页器

less分页器虽实用,但对于以表格形式展示的SQL结果集并非最佳选择。有一款开源工具名为pspg(其文档及源码可在GitHub上查阅),最初专为PostgreSQL开发,随后增加了对包括MariaDB在内的多种数据库的支持。由于mariadb SQL客户端能够连接到MariaDB Xpand数据库,我尝试了一番,效果完美。继续阅读,了解如何上手体验。

启动Xpand数据库最简便的方式是在SkySQL上创建服务(免费)。当然,你也可以通过Docker运行本地实例。以下是所需代码片段:

Shell

 

docker run --name xpand \
  -d \
  -p 3306:3306 \
  
--ulimit memlock=-1 \
  mariadb/xpand-single

数据库因数据而生动。该网站提供了一个既简单又有趣的演示数据库。在类Linux操作系统中,执行以下命令(若Xpand数据库部署于其他位置,请在最后一条命令中修改IP地址):

Shell

 

sudo apt install curl -y
curl https://www.mariadbtutorial.com/wp-content/uploads/2019/10/nation.zip --output nation.zip
unzip nation.zip
mariadb -h 127.0.0.1 -u xpand < nation.sql
rm nation.zip nation.sql

别忘了安装pspg

Shell

 

apt install pspg -y

使用mariadb SQL客户端连接数据库,并设置一个显示“Xpand”的定制且酷炫的提示符:

Shell

 

mariadb -h 127.0.0.1 -u xpand --prompt="Xpand [\d]> " nation

I learned this tip from my colleague Patrick Bossman (Product Manager at MariaDB) during a webinar on MariaDB Xpand + Docker. I recommend watching it if you want to learn more.

通过自定义提示连接到 MariaDB Xpand

为当前会话设置pspg分页器:

MariaDB SQL

 

pager pspg -s 14 -X --force-uniborder --quit-if-one-screen

A nice feature in pspg is that it shows the fancy text-based UI only when it makes sense (--quit-if-one-screen). So if your query returns only a few rows that fit in the screen, it will just show them right there on the screen as usual. For example, try running the following query:

MariaDB SQL

 

select * from continents;

此处无新内容可看。

如果仅显示少数几行,pspg分页器不会激活

但可以尝试以下操作:

MariaDB SQL

 

select * from countries;

A navigable text-based interface allows you to explore the data more efficiently.

pspg分页器渲染 MariaDB Xpand 的数据

您可以搜索行、排序、导出到 CSV、冻结列、标记行,甚至使用鼠标与工具交互,以及其他功能。

pspg中的一些菜单选项

I hope this tool helps you the next time you have to interact with a database via SSH and the command line. You can find more information about how to install pspg on your operating system, configuration options, and documentation on the GitHub repository for the project. If you want to learn more about distributed SQL and the MariaDB Xpand database, watch this short video, take a look at this datasheet, and explore some of the blog posts and documentation.

Source:
https://dzone.com/articles/using-the-postgressql-pager-with-mariadb-xpand