使用JDBC连接到postgres时可以指定模式吗?

可能吗? 我可以在连接url上指定它吗? 怎么做?

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

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

你可以像这样指定url:

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

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

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

如果在您的环境中可能,您也可以将用户的默认模式设置为您所需的模式:

 ALTER USER user_name SET search_path to 'schema' 

我不相信有一种方法来在连接string中指定模式。 看来你必须执行

 set search_path to 'schema' 

在连接之后指定模式。

从版本9.4开始 ,可以在连接string中使用currentSchema参数。

例如:

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

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

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

http://jdbc.postgresql.org/

不要忘记你可以在一个单独的声明中使用SET SCHEMA 'myschema'

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

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

DataSourcesetCurrentSchema

在实例化DataSource实现时,请查找设置当前/默认模式的方法。

例如,在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_" ); // <----------