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


当前回答

如果在你的环境中可能,你也可以将用户的默认模式设置为你想要的模式:

ALTER USER user_name SET search_path to 'schema'

其他回答

不要忘记SET SCHEMA 'myschema',你可以在一个单独的语句中使用它

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

从9.4开始,或者JDBC驱动程序的更早版本开始,就支持setSchema(String schemaName)方法。

我知道这个问题已经得到了回答,但是我在尝试为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

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

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

http://jdbc.postgresql.org/

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

例如:

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

在Go中使用"sql.DB"(注意带有下划线的搜索路径):

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