search_path如何影响标识符解析和“当前模式”?
是否可以定义默认在哪个模式中创建新表? (由“不合格的表名”引用。)
我已经看到了有关在 Postgres 中使用“搜索路径”的一些详细信息,但我认为它只在检索数据时起作用,而不是在创建数据时起作用。
我有一堆 SQL 脚本,它们创建了许多表。我不想修改脚本,而是想默认设置数据库在特定模式中创建表 - 当它们具有不合格的名称时。
这可能吗?
Is it possible to define in which schema new tables get created by default? (Referred by "unqualified table names".)
I've seen some details about using the "search path" in Postgres, but I think it only works while retrieving data, not creating.
I have a bunch of SQL scripts, which create many tables. Instead of modifying the scripts, I want to set the database create tables in a specific schema by default - when they have unqualified names.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么是架构搜索路径
search_path
?手册:
大胆强调我的。这解释了标识符解析。
“当前架构”(或“默认架构”)是每个文档:
大胆强调我的。系统架构
pg_temp
(当前会话临时对象的架构)和pg_catalog
自动成为搜索的一部分路径并按此顺序首先进行搜索。 手册:按照原文加粗强调。并且
pg_temp
位于此之前,除非将其放在不同的位置。怎么设置呢?
有多种方法可以设置运行时变量
search_path
- 或任何运行时变量。在配置文件中为所有数据库中的所有角色设置集群范围的默认值
postgresql.conf
(并重新加载)。小心一点!此设置的出厂默认值< /a> 是:
注意:
<块引用>
第一个元素指定与
要搜索当前用户。如果不存在这样的架构,则忽略该条目。
还有:
<块引用>
如果列表项之一是特殊名称
$user
,则架构如果存在,则替换为
CURRENT_USER
返回的名称这样的模式,并且用户拥有它的 USAGE 权限。 (如果不,
$user
被忽略。)使用
ALTER SYSTEM
从 Postgres 内部执行相同操作(Postgres 12 或更高版本)。 手册:将其设置为一个数据库的默认值:
将其设置为您连接的角色的默认值(集群范围内有效):
甚至(通常是最好的!)作为数据库中的角色的默认值:
在脚本顶部编写命令。或者在您的数据库会话中执行它:
为函数范围设置特定的
search_path
(以防止恶意用户使用足够的权限)。了解编写SECURITY DEFINER
手册中的安全功能。列表中较高的数字胜过较低的数字。
手册有更多的方法,例如设置环境变量或使用命令行选项。
如何检查当前设置?
检查配置文件
postgresql.conf
(和postgresql.auto.conf
)。或者(更方便)使用系统视图pg_file_settings< /code>
,它总结了所有配置文件的内容:
要查看会话中的当前设置:
或从系统视图中获取完整信息
pg_settings
:每个角色/数据库的设置存储在系统目录中
pg_db_role_setting
(默认情况下无)。检查:如何重置?
要重置:
重置: postgresql.org/docs/current/sql-reset.html" rel="nofollow noreferrer">手册:
如何知道这将重置为什么?
或在更改之前保存当前状态(可能与重置值不同)首先,
SET
将其恢复到该状态(而不是RESET
)。有一个方便的函数set_config()
接受一个变量,而不是SET
。示例 PL/pgSQL 代码块:或使用
设置本地
。这会持续到当前事务结束,这可能会超过函数的结束。细微差别!What is the schema search path
search_path
?The manual:
Bold emphasis mine. This explains identifier resolution.
The “current schema” (or “default schema”) is, per documentation:
Bold emphasis mine. The system schemas
pg_temp
(schema for temporary objects of the current session) andpg_catalog
are automatically part of the search path and searched first, in this order. The manual:Bold emphasis as per original. And
pg_temp
comes before that, unless it's put into a different position.How to set it?
There are various ways to set the runtime variable
search_path
- or any runtime variable, for that matter.Set a cluster-wide default for all roles in all databases in the configuration file
postgresql.conf
(and reload). Careful with that!The factory default for this setting is:
Note:
And:
Do the same from inside Postgres with
ALTER SYSTEM
(Postgres 12 or later). The manual:Set it as default for one database:
Set it as default for the role you connect with (effective cluster-wide):
Or even (often best!) as default for a role in a database:
Write the command at the top of your script. Or execute it in your DB session:
Set a specific
search_path
for the scope of a function (to be safe from malicious users with sufficient privileges). Read about WritingSECURITY DEFINER
Functions Safely in the manual.Higher number in the list trumps lower number.
The manual has even more ways, like setting environment variables or using command-line options.
How check current settings?
Inspect the configuration file(s)
postgresql.conf
(andpostgresql.auto.conf
). Or (more conveniently) use the system viewpg_file_settings
, which summarizes the contents of all configuration files:To see the current setting in your session:
Or get complete information from the system view
pg_settings
:Settings per role / database are stored in the system catalog
pg_db_role_setting
(none by default). To check:How reset it?
To reset:
The manual:
How to know what this will reset to?
Or save the current state (which may differ from the reset value) before you change it in the first place, and
SET
it back to that state (rather thanRESET
). There is the convenient functionset_config()
taking a variable, as opposed toSET
. Example PL/pgSQL code block:Or use
SET LOCAL
. That lasts till the end of the current transaction, which may be past the end of a function. Subtle difference!搜索路径确实是你想要的:
Search path is indeed what you want: