postgresql 如何切换schema
时间: 2023-10-01 07:02:15 浏览: 112
要切换到另一个Schema,可以使用以下命令:
```sql
SET search_path = schema_name;
```
其中,schema_name 是你想要切换到的 Schema 名称。
例如,如果你想要切换到名为 "sales" 的 Schema,可以使用以下命令:
```sql
SET search_path = sales;
```
该命令将把当前会话的搜索路径设置为 "sales" Schema。
如果你想要将搜索路径设置为多个 Schema,可以使用以下命令:
```sql
SET search_path = schema_name_1, schema_name_2, ...;
```
例如:
```sql
SET search_path = sales, accounting;
```
这将把搜索路径设置为 "sales" 和 "accounting" 两个 Schema。
相关问题
postgresql 切换到指定schema
要切换到指定的schema,可以使用以下命令:
```
SET search_path = schema_name, public;
```
其中,`schema_name`是要切换到的schema的名称。这个命令将把当前的schema设置为指定的schema和公共schema。
如果要永久设置默认的schema,可以在postgresql.conf文件中设置search_path参数。例如:
```
search_path = schema_name, public
```
这将在每次启动PostgreSQL时自动设置默认的schema。
postgresql currentschema
current_schema是PostgreSQL中的一个系统变量,它用于表示当前模式的名称。
在PostgreSQL中,一个数据库可以包含多个模式,每个模式中可以有多个表、视图、函数等数据库对象。使用current_schema变量可以获取当前会话正在使用的模式的名称。
current_schema可以有两种用法:
1. 如果直接使用SELECT current_schema;语句,在控制台或者SQL编辑器中执行时,会返回当前会话所设置的模式名称。
例如,如果当前设置的模式为public,那么执行SELECT current_schema;语句时,会返回public。
2. 在SQL语句中使用current_schema变量可以避免指定模式名称,提高SQL语句的可移植性。
例如,如果有一个表名为students的表,并且该表在public模式下和student模式下都存在,可以使用SELECT * FROM current_schema().students;语句来引用当前模式下的student表,而不需要指定具体的模式名称。
总之,current_schema是一个非常有用的系统变量,它可以方便地获取当前会话所设置的模式名称,避免硬编码模式名称,从而提高SQL语句的可移植性和灵活性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)