PG 中 .psqlrc 配置文件使用案例
文章目录
- .psqlrc 配置文件作用
- 1. 自动执行 SQL 命令和元命令
- 2. 设置默认行为和样式
- 3. 创建快捷命令和别名
- 4. 自动加载常用设置
- 示例 .psqlrc 文件内容:
- 文件位置:
- .psqlrc 配置文件路径
- Linux 环境示例
- 示例1
- 示例2
- Windows 环境示例
- 查看用户家目录
- 示例1
- 示例2
.psqlrc 配置文件作用
在 PostgreSQL 中,.psqlrc
文件是一个特殊的配置文件,它在每次启动 psql
命令行客户端时自动执行。这个文件的作用主要包括以下几个方面:
1. 自动执行 SQL 命令和元命令
你可以在 .psqlrc
文件中放置任何有效的 SQL 命令或 psql 元命令,这些命令会在每次启动 psql 时自动执行。这对于设置常用的环境配置非常有用。
2. 设置默认行为和样式
常见的用途包括:
- 设置输出格式(如对齐方式、边框样式等)
- 定义提示符格式
- 设置默认的搜索路径
- 配置时区或其他会话参数
3. 创建快捷命令和别名
可以定义 \alias
或使用 \set
创建快捷方式,简化常用查询或操作。
4. 自动加载常用设置
比如设置 timing on
来显示查询执行时间
示例 .psqlrc 文件内容:
-- 设置提示符显示当前数据库和时间
\set PROMPT1 '%n@%/%R%# '-- 显示查询执行时间
\timing on-- 设置 null 值的显示方式
\pset null '[NULL]'-- 设置输出格式
\x auto-- 设置搜索路径
SET search_path TO myschema, public;-- 定义快捷查询
\set customers 'SELECT * FROM customers LIMIT 10;'
这样,每次启动 psql 时都会自动应用这些设置,提高了工作效率,也保证了在不同会话中的一致性。
文件位置:
- Linux/Unix/MacOS:
$HOME/.psqlrc
- Windows:
%APPDATA%\postgresql\psqlrc.conf
或%USERPROFILE%\.psqlrc
当需要在所有 psql 会话中应用某些设置时,.psqlrc
是一个非常有用的工具。
.psqlrc 配置文件路径
linux 环境 postgresql 的 psql 预配置文件为 $HOME/.psqlrc
对应在 Windows 环境中,.psqlrc 文件通常位于以下位置之一:
- 用户主目录下:%APPDATA%\postgresql\psqlrc.conf
- 通常路径是:C:\Users{用户名}\AppData\Roaming\postgresql\psqlrc.conf 或者在用户的主目录下直接创建 .psqlrc 文件:
- 路径类似于:C:\Users{用户名}.psqlrc 可以通过设置 PSQLRC 环境变量来指定 .psqlrc 文件的位置。
Linux 环境示例
示例1
[postgres@ums-42 ~]$ psql -d postgres
psql (PostgreSQL 15.13 (PolarDB 15.13.4.0 build 1c8d76ee) on x86_64-linux-gnu)
Type "help" for help.postgres=# select 1;?column?
----------1
(1 row)postgres=# \q
[postgres@ums-42 ~]$ export PSQLRC=/home/postgres/test/.psqlrc
[postgres@ums-42 ~]$ psql -d postgres
Timing is on.
psql (PostgreSQL 15.13 (PolarDB 15.13.4.0 build 1c8d76ee) on x86_64-linux-gnu)
Type "help" for help.postgres=# select 1;?column?
----------1
(1 row)Time: 0.536 ms
postgres=# \q
[postgres@ums-42 ~]$
[postgres@ums-42 ~]$ cat /home/postgres/test/.psqlrc
\timing on
[postgres@ums-42 ~]$
示例2
[postgres@ums-42 ~]$ unset PSQLRC
[postgres@ums-42 ~]$ echo $PSQLRC[postgres@ums-42 ~]$ mv /home/postgres/test/.psqlrc /home/postgres/
[postgres@ums-42 ~]$ echo $HOME
/home/postgres
[postgres@ums-42 ~]$ cat /home/postgres/.psqlrc
\timing on
[postgres@ums-42 ~]$
[postgres@ums-42 ~]$ psql -d postgres
Timing is on.
psql (PostgreSQL 15.13 (PolarDB 15.13.4.0 build 1c8d76ee) on x86_64-linux-gnu)
Type "help" for help.postgres=# select 1;?column?
----------1
(1 row)Time: 0.513 ms
postgres=# \q
Windows 环境示例
查看用户家目录
echo %USERPROFILE%C:\Users\l11799>echo %USERPROFILE%
C:\Users\l11799C:\Users\l11799>
示例1
C:\Windows\system32>type %APPDATA%\postgresql\psqlrc.conf
\timing on
C:\Windows\system32>
C:\Windows\system32>C:\UNVGuard\Server\public\PostgreSQL\pgsql\bin\psql -Upostgres -d postgres -h 127.0.0.1
Timing is on.
psql (15.6)
Type "help" for help.postgres=# select 1;?column?
----------1
(1 row)Time: 0.499 ms
postgres=# \qC:\Windows\system32>
示例2
C:\Windows\system32>set PSQLRC=C:\Users\uniview\AppData\Roaming\postgresql\test.confC:\Windows\system32>C:\UNVGuard\Server\public\PostgreSQL\pgsql\bin\psql -Upostgres -d postgres -h 127.0.0.1
Timing is on.
psql (15.6)
Type "help" for help.postgres=# select 1;?column?
----------1
(1 row)Time: 0.543 ms
postgres=# \q