postgres psql script path
我正在制作一个有用的sql脚本目录来使用psql。 我希望能够使用它们而无需cd到目录。
是否可以配置psql来搜索特定路径以获取调用的脚本? 或者我是否必须使用完全限定的名称来调用它们?
是的,您可以使用启动文件
psqlrc and~/.psqlrc Unless it is passed an
-X or-c option, psql attempts to read and execute commands from the system-wide startup file (psqlrc ) and then
the user's personal startup file (~/.psqlrc ), after connecting to the
database but before accepting normal commands.
创建文件(如果它还不存在)并将psql元命令
每个文件:
\cd [ directory ] Changes the current working directory to
directory . Without argument, changes to the current user's home directory.
例如,将其放在用户
1 2 3 | \SET QUIET ON \cd /var/lib/postgres/script/ \SET QUIET OFF |
您只需将脚本放在.psqlrc文件中即可:
1 | \SET uptime 'select now() - backend_start as uptime from pg_stat_activity where pid = pg_backend_pid();' |
然后在psql中运行查询,前缀为冒号
要么
如果您的脚本很长并且您希望更好地组织它们,那么您可以创建脚本目录
1 | echo"select now() - backend_start as uptime from pg_stat_activity where pid = pg_backend_pid();"> ~/psqlrc.d/uptime.sql |
然后编辑你的?/ .psqlrc文件来添加:
1 | \SET uptime '\\i ~/psqlrc.d/uptime.sql' |
并再次通过在psql中键入