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

文章目录

  • [.psqlrc 配置文件作用](#.psqlrc 配置文件作用)
      • [1. 自动执行 SQL 命令和元命令](#1. 自动执行 SQL 命令和元命令)
      • [2. 设置默认行为和样式](#2. 设置默认行为和样式)
      • [3. 创建快捷命令和别名](#3. 创建快捷命令和别名)
      • [4. 自动加载常用设置](#4. 自动加载常用设置)
      • [示例 .psqlrc 文件内容:](#示例 .psqlrc 文件内容:)
      • 文件位置:
  • [.psqlrc 配置文件路径](#.psqlrc 配置文件路径)
  • [Linux 环境示例](#Linux 环境示例)
  • [Windows 环境示例](#Windows 环境示例)

.psqlrc 配置文件作用

在 PostgreSQL 中,.psqlrc 文件是一个特殊的配置文件,它在每次启动 psql 命令行客户端时自动执行。这个文件的作用主要包括以下几个方面:

1. 自动执行 SQL 命令和元命令

你可以在 .psqlrc 文件中放置任何有效的 SQL 命令或 psql 元命令,这些命令会在每次启动 psql 时自动执行。这对于设置常用的环境配置非常有用。

2. 设置默认行为和样式

常见的用途包括:

  • 设置输出格式(如对齐方式、边框样式等)
  • 定义提示符格式
  • 设置默认的搜索路径
  • 配置时区或其他会话参数

3. 创建快捷命令和别名

可以定义 \alias 或使用 \set 创建快捷方式,简化常用查询或操作。

4. 自动加载常用设置

比如设置 timing on 来显示查询执行时间

示例 .psqlrc 文件内容:

sql 复制代码
-- 设置提示符显示当前数据库和时间
\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 文件通常位于以下位置之一:

  1. 用户主目录下:%APPDATA%\postgresql\psqlrc.conf
  2. 通常路径是:C:\Users{用户名}\AppData\Roaming\postgresql\psqlrc.conf 或者在用户的主目录下直接创建 .psqlrc 文件:
  3. 路径类似于:C:\Users{用户名}.psqlrc 可以通过设置 PSQLRC 环境变量来指定 .psqlrc 文件的位置。

Linux 环境示例

示例1

sql 复制代码
[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

sql 复制代码
[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 环境示例

查看用户家目录

bash 复制代码
echo %USERPROFILE%

C:\Users\l11799>echo %USERPROFILE%
C:\Users\l11799

C:\Users\l11799>

示例1

bash 复制代码
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

bash 复制代码
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
相关推荐
endcy20161 天前
多路召回之-PGSQL的关键词检索分词插件安装
人工智能·ai·postgresql
QQ 19226381 天前
基于LabVIEW语言的ABB上位机Demo:获取日志、设备信息、速度与状态功能介绍
postgresql
雪域迷影1 天前
完整的后端课程 | NodeJS、ExpressJS、JWT、Prisma、PostgreSQL
数据库·postgresql·node.js·express·prisma
迷茫的21世纪的新轻年2 天前
PostgreSQL——SQL优化
数据库·sql·postgresql
2301_800256112 天前
8.3 查询优化 核心知识点总结
大数据·数据库·人工智能·sql·postgresql
mpHH2 天前
ivorysql 源码分析-双port兼容
数据库·学习·postgresql
南棱笑笑生2 天前
20251205在ubuntu20.04.6下的打包/解压缩tar.bz2压缩包的方法
数据库·postgresql
java_logo2 天前
PGADMIN4 Docker 容器化部署指南
运维·数据库·docker·postgresql·容器·数据库系统
瀚高PG实验室3 天前
postgresql日期/时间数据类型中有无时区的差异使用
数据库·postgresql·瀚高数据库
Elastic 中国社区官方博客3 天前
EDB EPAS 通过 PostgreSQL 连接器同步数据到 Elasticsearch
大数据·数据库·人工智能·elasticsearch·搜索引擎·postgresql·全文检索