重庆思庄Oracle、KingBase、PostgreSQL、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 224|回复: 0
打印 上一主题 下一主题

PG 中 .psqlrc 配置文件使用案例

[复制链接]
跳转到指定楼层
楼主
发表于 2025-11-9 13:56:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
.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\l11799

C:\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=# \q

C:\Windows\system32>
示例2

C:\Windows\system32>set PSQLRC=C:\Users\uniview\AppData\Roaming\postgresql\test.conf

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.543 ms
postgres=# \q

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2026-4-18 00:16 , Processed in 0.212446 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表