Java 使用 JDBC 连接到 postgres 时是否可以指定架构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4168689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:15:18  来源:igfitidea点击:

Is it possible to specify the schema when connecting to postgres with JDBC?

javadatabasepostgresqljdbcdatabase-schema

提问by marcosbeirigo

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

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

采纳答案by Hiro2k

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.

我知道这已经得到了回答,但是我在尝试指定要用于 liquibase 命令行的模式时遇到了同样的问题。

UpdateAs of JDBC v9.4you can specify the url with the new currentSchema parameter like so:

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

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

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

回答by Herks

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.

建立连接后指定架构。

回答by chzbrgla

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'

回答by Scott Langley

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:

几年前,我向 PostgreSQL JDBC 驱动程序提交了补丁的更新版本以启用此功能。您必须从源代码构建 PostreSQL JDBC 驱动程序(在添加补丁后)才能使用它:

http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php

http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php

http://jdbc.postgresql.org/

http://jdbc.postgresql.org/

回答by beldaz

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

不要忘记SET SCHEMA 'myschema'您可以在单独的 Statement 中使用哪个

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

SET SCHEMA 'value' 是 SET search_path TO 值的别名。使用此语法只能指定一个模式。

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

由于 JDBC 驱动程序的 9.4 和可能更早的版本,因此支持该setSchema(String schemaName)方法。

回答by Distortum

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

版本 9.4 开始,您可以currentSchema在连接字符串中使用该参数。

For example:

例如:

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

回答by Basil Bourque

DataSourcesetCurrentSchema

DataSource—— setCurrentSchema

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

在实例化一个DataSource实现时,寻找一种设置当前/默认模式的方法。

For example, on the PGSimpleDataSourceclass call setCurrentSchema.

例如,在PGSimpleDataSource课堂上调用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 publicwithin the database. See the manual, section 5.9.2 The Public Schema. To quote hat manual:

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

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.

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

回答by Rafael Barros

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

在 Go with "sql.DB" 中(注意search_path带下划线):

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

回答by AlexSandu75

This was already answered :

这已经回答了:

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

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

As in previous answers , the above connection string just works .

与前面的答案一样,上面的连接字符串可以正常工作。

I've checked , and it's OK : https://youtu.be/m0lBUHSLkNM?t=79

我检查过了,没问题:https: //youtu.be/m0lBUHSLkNM?t=79

( Although the accepted answer was given 8 years ago , it was edited 1 year ago . . . )

(虽然接受的答案是 8 年前给出的,但它是 1 年前编辑的......)