Is it possible to specify the schema when connecting to postgres with JDBC?
可能吗? 我可以在连接URL上指定它吗? 怎么做?
我知道已经解决了这个问题,但是我遇到了同样的问题,试图指定用于liquibase命令行的架构。
更新资料
从JDBC v9.4开始,您可以使用新的currentSchema参数指定url,如下所示:
1 | 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是这样的:
1 | jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema |
从9.4版开始,您可以在连接字符串中使用
例如:
1 | jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema |
如果在您的环境中可行,您还可以将用户的默认架构设置为所需的架构:
1 | ALTER USER user_name SET search_path to 'schema' |
我不相信有一种方法可以在连接字符串中指定架构。看来您必须执行
1 | set search_path to 'schema' |
建立连接后指定架构。
我将修补程序的更新版本提交给PostgreSQL JDBC驱动程序,以实现此功能。您必须从源代码构建PostreSQL JDBC驱动程序(在添加补丁之后)才能使用它:
http://archives.postgresql.org/pgsql-jdbc/2008-07/msg00012.php
http://jdbc.postgresql.org/
实例化
例如,在
1 2 3 4 5 6 7 | 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将尝试连接到名为
不要忘记
SET SCHEMA 'value' is an alias for SET search_path TO value. Only one
schema can be specified using this syntax.
而且由于JDBC驱动程序上的9.4及更早版本,因此支持
在Go with" sql.DB"中(注意带下划线的
1 | postgres://user:password@host/dbname?sslmode=disable&search_path=schema |
这已经被回答:
jdbc:postgresql:// localhost:5432 / mydatabase?currentSchema = myschema
与前面的答案一样,上述连接字符串也有效。
我已经检查了,没关系:
(尽管公认的答案是在8年前给出的,
它是1年前编辑的。 。 。 )