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


当前回答

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

例如:

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

其他回答

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

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

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

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”的模式中。每个新数据库都包含这样一个模式。

几年前,我向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