在 psql 中绘图

如果你像我一样主要在 psql 中使用 Postgres,那么你可能希望能够在不离开 psql 的情况下绘制一个简单的图表。

以下方法最初由 Alexander Korotkov 描述,我对其进行了略微调整以适配Python3。此方法得益于 iTerm2的内嵌图片协议。对于 Linux,有其他方式可以实现类似效果 (例如,How do I make my terminal display graphical pictures?)。

  1. 获取绘图脚本并安装 matplotlib:
Shell
wget \
  -O ~/pg_graph.py \
  https://gist.githubusercontent.com/NikolayS/d5f1af808f7275dc1491c37fb1e2dd11/raw/4f19a23222a6f7cf66eead3cae9617dd39bf07a5/pg_graph

pip install matplotlib
  1. 在 ~/.psqlrc 中定义一个宏 (在 bash、zsh 和 csh 中均可工作):
Shell
printf "%s %s %s %s %s %s\n" \\set graph \'\\\\g \|
python3 $(pwd)/pg_graph.py\' \
  >> ~/.psqlrc
  1. 启动 psql 并尝试一下
PSQL
nik=# with avg_temp(month, san_diego, vancouver, london) as (
    values
      ('Jan', 15, 4, 5),
      ('Feb', 16, 5, 6),
      ('Mar', 17, 7, 8),
      ('Apr', 18, 10, 11),
      ('May', 19, 14, 15),
      ('Jun', 21, 17, 17),
      ('Jul', 24, 20, 19),
      ('Aug', 25, 21, 20),
      ('Sep', 23, 18, 17),
      ('Oct', 21, 12, 13),
      ('Nov', 18, 8, 8),
      ('Dec', 16, 5, 6)
  )
  select * from avg_temp;

 month | san_diego | vancouver | london
-------+-----------+-----------+--------
 Jan   |        15 |         4 |      5
 Feb   |        16 |         5 |      6
 Mar   |        17 |         7 |      8
 Apr   |        18 |        10 |     11
 May   |        19 |        14 |     15
 Jun   |        21 |        17 |     17
 Jul   |        24 |        20 |     19
 Aug   |        25 |        21 |     20
 Sep   |        23 |        18 |     17
 Oct   |        21 |        12 |     13
 Nov   |        18 |         8 |      8
 Dec   |        16 |         5 |      6
(12 rows)

nik=# :graph

结果:

Graph result

讨论与补充

在 GitHub 查看讨论

支持作者

感谢支持 PostgreSQL Howto 的长期维护。

微信赞赏码

读者交流群

二维码若已过期,可通过公众号 PostgreSQL 学徒或邮件联系。

PostgreSQL 学徒读者交流群二维码