使用 JDBC 连接 postgres 时是否可以指定模式?
是否可以?我可以在连接 URL 上指定它吗?怎么做呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以?我可以在连接 URL 上指定它吗?怎么做呢?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
我知道这个问题已经得到解答,但我刚刚遇到了同样的问题,试图指定用于 liquibase 命令行的架构。
更新
从 JDBC v9.4 开始,您可以使用新的 url 指定 url currentSchema 参数如下所示:
基于早期补丁显示:
http://web.archive.org/web/20141025044151/http://postgresql.1045698.n5.nabble .com/Patch-to-allow-setting-schema-search-path-in-the-connectionURL-td2174512.html
其中建议的网址如下所示:
I know this was answered already, but I just ran into the same issue trying to specify the schema to use for the liquibase command line.
Update
As of JDBC v9.4 you can specify the url with the new currentSchema parameter like so:
Appears based on an earlier patch:
http://web.archive.org/web/20141025044151/http://postgresql.1045698.n5.nabble.com/Patch-to-allow-setting-schema-search-path-in-the-connectionURL-td2174512.html
Which proposed url's like so:
从版本 9.4 开始,您可以使用
currentSchema
参数< /a> 在你的连接字符串中。例如:
As of version 9.4, you can use the
currentSchema
parameter in your connection string.For example:
如果您的环境可行,您还可以将用户的默认架构设置为您所需的架构:
If it is possible in your environment, you could also set the user's default schema to your desired schema:
我不相信有一种方法可以在连接字符串中指定架构。看来您必须
在连接建立后执行才能指定架构。
I don't believe there is a way to specify the schema in the connection string. It appears you have to execute
after the connection is made to specify the schema.
DataSource
–setCurrentSchema
实例化
DataSource
实现,寻找设置当前/默认架构的方法。例如,在
PGSimpleDataSource
< /a> 类调用setCurrentSchema
。如果未指定架构,Postgres 将默认使用数据库中名为
public
的架构。请参阅手册第 5.9.2 节 公共架构。引用帽子手册:DataSource
–setCurrentSchema
When instantiating a
DataSource
implementation, look for a method to set the current/default schema.For example, on the
PGSimpleDataSource
class callsetCurrentSchema
.If you leave the schema unspecified, Postgres defaults to a schema named
public
within the database. See the manual, section 5.9.2 The Public Schema. To quote hat manual:在 Go 中使用“sql.DB”(注意带有下划线的
search_path
):In Go with "sql.DB" (note the
search_path
with underscore):几年前,我向 PostgreSQL JDBC 驱动程序提交了补丁的更新版本以启用此功能。您必须从源代码构建 PostreSQL JDBC 驱动程序(添加补丁后)才能使用它:
http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php
http://jdbc.postgresql .org/
I submitted an updated version of a patch to the PostgreSQL JDBC driver to enable this a few years back. You'll have to build the PostreSQL JDBC driver from source (after adding in the patch) to use it:
http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php
http://jdbc.postgresql.org/
我已经尝试过: key-value 部分中的 currentSchema、searchpath、search_path ,但没有任何效果对我有用。
我找到了使用“options”关键字的解决方法:
postgresql://host:port/dbname?options=-c%20search_path%3Dschema_name
I have tried: currentSchema, searchpath, search_path in the key-value section and nothing works for me.
I have found work around with the "options" key word:
postgresql://host:port/dbname?options=-c%20search_path%3Dschema_name
不要忘记
SET SCHEMA 'myschema'
< /a> 您可以在单独的语句中使用它从 9.4 版以及可能更早的 JDBC 驱动程序版本开始,支持
setSchema(String schemaName)
方法。Don't forget
SET SCHEMA 'myschema'
which you could use in a separate StatementAnd since 9.4 and possibly earlier versions on the JDBC driver, there is support for the
setSchema(String schemaName)
method.于 2024 年更新
如果要设置架构,请使用
?options=-c%20search_path=
完整 url
根据 postgresql JDBC 文档
https://jdbc.postgresql.org/documentation/use/
Updated on 2024
If you want to set schema, use
?options=-c%20search_path=
Full url
According to the postgresql JDBC doc
https://jdbc.postgresql.org/documentation/use/