使用 JDBC 连接 postgres 时是否可以指定模式?

发布于 2024-10-02 13:27:41 字数 34 浏览 1 评论 0 原文

是否可以?我可以在连接 URL 上指定它吗?怎么做呢?

Is it possible? Can i specify it on the connection URL? How to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

蝶舞 2024-10-09 13:27:41

我知道这个问题已经得到解答,但我刚刚遇到了同样的问题,试图指定用于 liquibase 命令行的架构。

更新
从 JDBC v9.4 开始,您可以使用新的 url 指定 url currentSchema 参数如下所示:

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema

基于早期补丁显示:

http://web.archive.org/web/20141025044151/http://postgresql.1045698.n5.nabble .com/Patch-to-allow-setting-schema-search-path-in-the-connectionURL-td2174512.html

其中建议的网址如下所示:

jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema

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:

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema

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:

jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
奢华的一滴泪 2024-10-09 13:27:41

从版本 9.4 开始,您可以使用 currentSchema 参数< /a> 在你的连接字符串中。

例如:

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema

As of version 9.4, you can use the currentSchema parameter in your connection string.

For example:

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
泛泛之交 2024-10-09 13:27:41

如果您的环境可行,您还可以将用户的默认架构设置为您所需的架构:

ALTER USER user_name SET search_path to 'schema'

If it is possible in your environment, you could also set the user's default schema to your desired schema:

ALTER USER user_name SET search_path to 'schema'
白云不回头 2024-10-09 13:27:41

我不相信有一种方法可以在连接字符串中指定架构。看来您必须

set search_path to 'schema'

在连接建立后执行才能指定架构。

I don't believe there is a way to specify the schema in the connection string. It appears you have to execute

set search_path to 'schema'

after the connection is made to specify the schema.

小梨窩很甜 2024-10-09 13:27:41

DataSourcesetCurrentSchema

实例化 DataSource 实现,寻找设置当前/默认架构的方法。

例如,在 PGSimpleDataSource< /a> 类调用 setCurrentSchema

org.postgresql.ds.PGSimpleDataSource dataSource = new org.postgresql.ds.PGSimpleDataSource ( );
dataSource.setServerName ( "localhost" );
dataSource.setDatabaseName ( "your_db_here_" );
dataSource.setPortNumber ( 5432 );
dataSource.setUser ( "postgres" );
dataSource.setPassword ( "your_password_here" );
dataSource.setCurrentSchema ( "your_schema_name_here_" );  // <----------

如果未指定架构,Postgres 将默认使用数据库中名为 public 的架构。请参阅手册第 5.9.2 节 公共架构。引用帽子手册:

在前面的部分中,我们创建了表,但没有指定任何架构名称。默认情况下,此类表(和其他对象)会自动放入名为“public”的模式中。每个新数据库都包含这样的模式。

DataSourcesetCurrentSchema

When instantiating a DataSource implementation, look for a method to set the current/default schema.

For example, on the PGSimpleDataSource class call setCurrentSchema.

org.postgresql.ds.PGSimpleDataSource dataSource = new org.postgresql.ds.PGSimpleDataSource ( );
dataSource.setServerName ( "localhost" );
dataSource.setDatabaseName ( "your_db_here_" );
dataSource.setPortNumber ( 5432 );
dataSource.setUser ( "postgres" );
dataSource.setPassword ( "your_password_here" );
dataSource.setCurrentSchema ( "your_schema_name_here_" );  // <----------

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:

In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema.

鹿港巷口少年归 2024-10-09 13:27:41

在 Go 中使用“sql.DB”(注意带有下划线的 search_path):

postgres://user:password@host/dbname?sslmode=disable&search_path=schema

In Go with "sql.DB" (note the search_path with underscore):

postgres://user:password@host/dbname?sslmode=disable&search_path=schema
情归归情 2024-10-09 13:27:41

几年前,我向 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/

笙痞 2024-10-09 13:27:41

我已经尝试过: 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

柠檬 2024-10-09 13:27:41

不要忘记 SET SCHEMA 'myschema'< /a> 您可以在单独的语句中使用它

SET SCHEMA“值”是 SET search_path TO 值的别名。只有一个
可以使用此语法指定架构。

从 9.4 版以及可能更早的 JDBC 驱动程序版本开始,支持 setSchema(String schemaName) 方法。

Don't forget SET SCHEMA 'myschema' which you could use in a separate Statement

SET SCHEMA 'value' is an alias for SET search_path TO value. Only one
schema can be specified using this syntax.

And since 9.4 and possibly earlier versions on the JDBC driver, there is support for the setSchema(String schemaName) method.

硪扪都還晓 2024-10-09 13:27:41

于 2024 年更新

如果要设置架构,请使用 ?options=-c%20search_path=

完整 url

jdbc:postgresql://localhost:5432/postgres?options=-c%20search_path=test,public,pg_catalog

根据 postgresql JDBC 文档
https://jdbc.postgresql.org/documentation/use/

Updated on 2024

If you want to set schema, use ?options=-c%20search_path=

Full url

jdbc:postgresql://localhost:5432/postgres?options=-c%20search_path=test,public,pg_catalog

According to the postgresql JDBC doc
https://jdbc.postgresql.org/documentation/use/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文