psql 调优
本页目录
.psqlrc
文件 ~/.psqlrc 可以用于设置一些默认配置。例如:
echo '\timing on' >> ~/.psqlrc
现在,如果我们启动 psql:
❯ psql -U postgres
Timing is on.
psql (15.5 (Debian 15.5-1.pgdg120+1))
Type "help" for help.
nik=# select pg_sleep(.5);
pg_sleep
----------
(1 row)
Time: 508.187 ms
❗**重要提示:**如果脚本涉及到 psql,建议使用 -X 选项来忽略 ~/.psqlrc 中的设置,这样脚本逻辑 (例如,分析 psql 的输出) 不会依赖于 ~/.psqlrc 中的任何配置。
pspg
pspg 非常棒。如果可能的话,建议安装。
例如,对于查询 select * from pg_class limit 3;,安装前:

安装后:


安装方法:
- macOS/Homebrew:
brew install pspg - Ubuntu/Debian:
sudo apt update && sudo apt install -y pspg
然后:
echo "\setenv PAGER pspg
\pset border 2
\pset linestyle unicode
\set x '\setenv PAGER less'
\set xx '\setenv PAGER \'pspg -bX --no-mouse\''
" >> ~/.psqlrc
NULLs
默认情况下,psql 输出中的 NULL 值是不可见的:
nik=# select null;
?column?
----------
(1 row)
可以使用以下设置,并将其写入 ~/.psqlrc,使后续 psql 会话也生效:
\pset null 'Ø'
现在 NULL 值将显示为:
nik=# select null;
?column?
----------
Ø
(1 row)
postgres_dba
postgres_dba 是我个人收集的 psql 脚本集,带有菜单支持:
