search_path 的使用说明

文章目录

文档用途

search_path 模式搜索路径,是数据库使用的一个进行表查看的模式列表,本文章用于介绍如何使用模式搜索路径参数 search_path 。

详细信息

在 HGDB 中,想要查看数据库中有什么数据表或者视图,一般会在连接数据库后使用元命令\d进行查询,而元命令\d的搜索范围会限定在这个 search_path 模式搜索路径参数中。

search_path 参数默认是 "$user", public 两个值。

sql 复制代码
highgo=# show search_path; 
   search_path   

-----------------

 "$user", public

(1 row)

$user 是指和当前用户的用户名相同的模式,public 就是数据库自带的 public 模式

在数据库中创建测试模式和数据表进行测试:

sql 复制代码
highgo=# create schema test_schema ;

CREATE SCHEMA

highgo=# create table test_table_public (id int );

CREATE TABLE

highgo=# create table test_schema.test_table_test_schema (id int );

CREATE TABLE

在默认的 search_path 参数下使用元命令 \d 查询表是不能把刚建立的表完全查询出来

sql 复制代码
highgo=# \d

                 List of relations

 Schema |           Name           | Type  | Owner  

--------+--------------------------+-------+--------

 public | pg_stat_statements       | view  | highgo

 public | pg_wait_sampling_current | view  | highgo

 public | pg_wait_sampling_history | view  | highgo

 public | pg_wait_sampling_profile | view  | highgo

 public | test_table_public        | table | highgo

(19 rows)

无法查询出来表 test_table_test_schema ,原因是数据库搜索是按照模式搜索路径中所列出来的模式进行搜索

将模式搜索路径参数进行暂时性修改:

sql 复制代码
highgo=# set search_path TO public,test_schema;

SET

或者

sql 复制代码
highgo=# set search_path = public,test_schema;

SET



highgo=# show search_path ;

     search_path     

---------------------

 public, test_schema

(1 row)

再次进行查询:

sql 复制代码
highgo=# \d

                    List of relations

   Schema    |           Name           | Type  | Owner  

-------------+--------------------------+-------+--------

public      | pg_wait_sampling_current | view  | highgo

 public      | pg_wait_sampling_history | view  | highgo

 public      | pg_wait_sampling_profile | view  | highgo

public      | test_table_public        | table | highgo

 test_schema | test_table_test_schema   | table | highgo

但是上述修改方式是暂时性修改,只会在本次会话生效,如果断开了数据库连接,再次使用 psql 连接进去数据库,search_path 参数又会变回默认值

search_path 参数是存在于 postgresql.conf 配置文件中的,如果想要永久生效则可以直接修改 postgresql.conf 配置文件:

sql 复制代码
[highgo@localhost ~]$ vi $PGDATA/postgresql.conf



#search_path = '"$user", public'        # schema names

去掉注释符号,将 search_path 参数的值修改为需要的值即可,不需要重启数据库生效

sql 复制代码
[highgo@localhost ~]$ pg_ctl reload


server signaled



highgo=# show search_path ;

         search_path         

-----------------------------

 "$user", public,test_schema

(1 row)
相关推荐
剩下了什么4 小时前
MySQL JSON_SET() 函数
数据库·mysql·json
山峰哥4 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器
较劲男子汉5 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
java搬砖工-苤-初心不变5 小时前
MySQL 主从复制配置完全指南:从原理到实践
数据库·mysql
山岚的运维笔记7 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
roman_日积跬步-终至千里7 小时前
【LangGraph4j】LangGraph4j 核心概念与图编排原理
java·服务器·数据库
汇智信科7 小时前
打破信息孤岛,重构企业效率:汇智信科企业信息系统一体化运营平台
数据库·重构
野犬寒鸦8 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
晚霞的不甘9 小时前
揭秘 CANN 内存管理:如何让大模型在小设备上“轻装上阵”?
前端·数据库·经验分享·flutter·3d
市场部需要一个软件开发岗位9 小时前
JAVA开发常见安全问题:纵向越权
java·数据库·安全