这可能吗?我可以在连接URL上指定它吗?怎么做呢?
当前回答
我知道这个问题已经得到了回答,但是我在尝试为liquibase命令行指定模式时遇到了同样的问题。
更新 从JDBC v9.4开始,你可以像这样用新的currentSchema参数指定url:
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
建议的url是这样的:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
其他回答
如果在你的环境中可能,你也可以将用户的默认模式设置为你想要的模式:
ALTER USER user_name SET search_path to 'schema'
数据源 – 设置当前模式
在实例化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_" ); // <----------
如果不指定模式,Postgres在数据库中默认使用名为public的模式。参见手册中的5.9.2 Public Schema章节。引用帽子手册:
在前面的部分中,我们创建表时没有指定任何模式名。默认情况下,这些表(和其他对象)被自动放入一个名为“public”的模式中。每个新数据库都包含这样一个模式。
我认为没有办法在连接字符串中指定模式。看来你得执行了
set search_path to 'schema'
在建立连接以指定模式之后。
我知道这个问题已经得到了回答,但是我在尝试为liquibase命令行指定模式时遇到了同样的问题。
更新 从JDBC v9.4开始,你可以像这样用新的currentSchema参数指定url:
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
建议的url是这样的:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
在Go中使用"sql.DB"(注意带有下划线的搜索路径):
postgres://user:password@host/dbname?sslmode=disable&search_path=schema
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 查询JSON类型内的数组元素
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 获得PostgreSQL数据库中当前连接数的正确查询
- MySQL数据库表中的最大记录数
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- 从现有模式生成表关系图(SQL Server)
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?