修复 psql 中的 \\e 编辑器问题

有时在 psql 中使用 \e 命令编辑查询时会遇到以下错误:

PSQL
nik=# \e
/usr/bin/sensible-editor: 20: editor: not found
/usr/bin/sensible-editor: 31: nano: not found
/usr/bin/sensible-editor: 20: nano-tiny: not found
/usr/bin/sensible-editor: 20: vi: not found
Couldn't find an editor!
Set the $EDITOR environment variable to your desired editor.

设置编辑器非常简单 (使用 nano 或喜欢的其他编辑器):

SQL
\setenv PSQL_EDITOR vim

但是,如果你在容器内或一台新机器上工作,可能还未安装所需的编辑器。假设有权限运行安装命令,可以直接在 psql 中安装编辑器。例如,在基于 Debian 的标准 Postgres 环境中 (不需要 sudo):

PSQL
nik=# \! apt update && apt install -y vim

👉 \e 命令就能正常工作了!

要持久化该设置,可以将其添加到 ~/.bash_profile (或 ~/.zprofile) 中:

Shell
echo "export PSQL_EDITOR=vim" >> ~/.bash_profile
source ~/.bash_profile

对于 Window,请参见 @PavloGolub 的博客文章。

文档: https://postgresql.org/docs/current/app-psql.html

讨论与补充

在 GitHub 查看讨论

支持作者

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

微信赞赏码

读者交流群

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

PostgreSQL 学徒读者交流群二维码