246

Is it possible? Can i specify it on the connection URL? How to do that?

10 Answers 10

342

I know this was answered already, but I just ran into the same issue trying to specify the schema to use for the liquibase command line.

Update As of JDBC v9.4 you can specify the url with the new currentSchema parameter like so:

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

Appears based on an earlier patch:

http://web.archive.org/web/20141025044151/http://postgresql.1045698.n5.nabble.com/Patch-to-allow-setting-schema-search-path-in-the-connectionURL-td2174512.html

Which proposed url's like so:

jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
Sign up to request clarification or add additional context in comments.

9 Comments

Yes but at the moment of the writing (late 2012) it's not a part of the 9.1 driver, see: Connection Parameters.
Did you try it? Because it wasn't listed as part of the previous driver but it still worked.
Tried with 9.3-1101-jdbc41 and 9.1, doesn't work for me
@IgnacioA.Poletti Try using the JDCB setSchema method after creating your connection. Works for me with a recent postgres driver.
We solved this problem by also using a different (newer) JDBC driver. In our case postgresql-9.4.1209.jdbc42.jar worked together with a 9.5 database and the ?currentSchema=myschema syntax.
|
92

As of version 9.4, you can use the currentSchema parameter in your connection string.

For example:

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

Comments

56

If it is possible in your environment, you could also set the user's default schema to your desired schema:

ALTER USER user_name SET search_path to 'schema'

1 Comment

Probably better to ALTER the database itself so that the same user can connect to different databases with different search_paths if need be: ALTER DATABASE dbname SET search_path TO public,schemaname;
46

I don't believe there is a way to specify the schema in the connection string. It appears you have to execute

set search_path to 'schema'

after the connection is made to specify the schema.

3 Comments

This worked for me, specifically using the "Connection" instance to run: Statement statement = connection.createStatement(); try { statement.execute("set search_path to '" + schema + "'"); } finally { statement.close(); }
There is a way to specify the default schema in the connection string (jdbc uri). See answers below.
This was probably correct at the point in time it was written (14 years ago), but it has been incorrect for the last decade (9.4.0 was released 2014-12-18).
11

DataSourcesetCurrentSchema

When instantiating a DataSource implementation, look for a method to set the current/default schema.

For example, on the PGSimpleDataSource class call 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_" );  // <----------

If you leave the schema unspecified, Postgres defaults to a schema named public within the database. See the manual, section 5.9.2 The Public Schema. To quote hat manual:

In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema.

1 Comment

"attempts to connect to a schema" - That's a bit misleading. The driver does not connect "to a schema", but to a database. Which schema is used by queries depends on the current setting of the search_path
9

I have tried: currentSchema, searchpath, search_path in the key-value section and nothing works for me. I have found work around with the "options" key word:

postgresql://host:port/dbname?options=-c%20search_path%3Dschema_name

1 Comment

asyncpg doesn't support it.
8

I submitted an updated version of a patch to the PostgreSQL JDBC driver to enable this a few years back. You'll have to build the PostreSQL JDBC driver from source (after adding in the patch) to use it:

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

http://jdbc.postgresql.org/

Comments

8

In Go with "sql.DB" (note the search_path with underscore):

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

Comments

5

Don't forget SET SCHEMA 'myschema' which you could use in a separate Statement

SET SCHEMA 'value' is an alias for SET search_path TO value. Only one schema can be specified using this syntax.

And since 9.4 and possibly earlier versions on the JDBC driver, there is support for the setSchema(String schemaName) method.

1 Comment

To expand, setting the schema on Datasource will ensure the connection automatically gets SET SESSION SCHEMA 'myschema'.
5

According to the official Postgresql JDBC doc, there are at least two connection options available for use on the JDBC URL to configure the schema to use.

The most straight forward option is using the currentSchema property, as shown in Steve's answer.

You also have the possibility of providing a URL encoded string of of options to be used by the CLI to configure the connection. The most straight forward way of configuring this is using the Java API, as that will take care of all URL encoding, such as swapping spaces for "%20", "=" for "%3D", etc.

Properties props = new Properties();
props.setProperty("options", "-c search_path=test,public,pg_catalog -c statement_timeout=90000");
Connection conn = DriverManager.getConnection(url, props);

This will result in the query parameter "options" have the value c+search_path%3Dtest%2Cpublic%2Cpg_catalog+-c+statement_timeout%3D90000. Not exactly nice to hand code, but you can do it :) If you only require to set the schema, you can get away with just appending ?options=-c%20search_path= to the URL:

jdbc:postgresql://localhost:5432/postgres?options=-c%20search_path=test,public,pg_catalog

Additionally, the JDBC API provides a setter for the current schema on Datasource, which is what Hikari is using when setting schema in Spring Boot using the spring.datasource.hikari.schema property.

2 Comments

Thanks for your helpful edits @oligofren, it's much clearer now. But I found out that languages like C# and Go failed when using currentSchema. The only solution is to use ?options=-c%20search_path=.
The currentSchema property has nothing to do with Postgres, so that's no wonder. It is a property of the JDBC API and it is up to the implementation for your specific database to figure out what to do with it. In the Postgres driver it uses this property simply to execute format('SET SEARCH_PATH=%s', currentSchema). So of course that does not work by itself in other languages, but you can use the same approach in Go or C#: just execute 'SET SEARCH_PATH=${searchPath}' and you are done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.