有可能吗?我可以在连接URL中指定它吗?怎样做?
有可能吗?我可以在连接URL中指定它吗?怎样做?
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
这似乎基于之前的补丁:
类似以下提议的网址:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
从9.4版本开始,您可以在连接字符串中使用currentSchema
参数。
例如:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
如果您的环境允许,您还可以将用户的默认模式设置为所需的模式:
ALTER USER user_name SET search_path to 'schema'
我认为没有一种方法可以在连接字符串中指定模式。看起来你必须执行
set search_path to 'schema'
在连接建立之后指定模式。
DataSource
– setCurrentSchema
在实例化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_" ); // <----------
public
的模式。请参考手册5.9.2节The Public Schema。引用该手册的话:search_path
的当前设置。 - user330315使用“sql.DB”在Go中(请注意下划线的search_path
):
postgres://user:password@host/dbname?sslmode=disable&search_path=schema
几年前,我提交了一个更新版本的补丁给PostgreSQL JDBC驱动程序,以启用此功能。您需要从源代码构建PostgreSQL JDBC驱动程序(添加补丁后)才能使用它:
http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php
SET SCHEMA 'myschema'
,这可以在一个独立的语句中使用。
自从9.4和可能更早版本的JDBC驱动程序开始,就支持SET SCHEMA 'value'是SET search_path TO value的别名。只能使用这个语法指定一个模式。
setSchema(String schemaName)
方法。我已经尝试了在键值部分中使用currentSchema、searchpath和search_path,但都无法解决我的问题。
我找到了一个临时解决方法,使用"options"关键字:
postgresql://host:port/dbname?options=-c%20search_path%3Dschema_name
psql
命令行工作 - undefined
setSchema
方法。最近的Postgres驱动程序对我有效。 - beldazpostgresql-9.4.1209.jdbc42.jar
与9.5
数据库和?currentSchema=myschema
语法一起工作。 - SebastianH