Is it possible to specify the schema when connecting to postgres with JDBC?
Solution 1
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:
Which proposed url's like so:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
Solution 2
As of version 9.4, you can use the currentSchema
parameter in your connection string.
For example:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
Solution 3
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'
Solution 4
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.
Solution 5
DataSource
– setCurrentSchema
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.
Related videos on Youtube
marcosbeirigo
Updated on May 06, 2021Comments
-
marcosbeirigo about 3 years
Is it possible? Can i specify it on the connection URL? How to do that?
-
user272735 over 11 yearsYes but at the moment of the writing (late 2012) it's not a part of the 9.1 driver, see: Connection Parameters.
-
Hiro2k over 11 yearsDid you try it? Because it wasn't listed as part of the previous driver but it still worked.
-
Ignacio A. Poletti almost 10 yearsTried with 9.3-1101-jdbc41 and 9.1, doesn't work for me
-
Flowchartsman over 9 yearsProbably 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;
-
romeara over 9 yearsThis 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(); }
-
acorello almost 9 yearsThere is a way to specify the default schema in the connection string (jdbc uri). See answers below.
-
beldaz almost 9 years@IgnacioA.Poletti Try using the JDCB
setSchema
method after creating your connection. Works for me with a recent postgres driver. -
pablo.vix over 8 yearssearchpath didn't work.... using 9.4. see: jdbc.postgresql.org/documentation/94/…
-
Hiro2k over 8 yearsIt looks like they added a new currentSchema according to that link.
-
Erhannis almost 8 yearsNeither of these work for me. When I prepend "schema." to my table names, it DOES work, so it should indeed be a problem of just not being in the right schema, but neither of the above options themselves appear to set the schema. PostgreSQL 9.5
-
SebastianH over 7 yearsWe solved this problem by also using a different (newer) JDBC driver. In our case
postgresql-9.4.1209.jdbc42.jar
worked together with a9.5
database and the?currentSchema=myschema
syntax. -
DuSant about 7 yearsIt worked for me for JDBC version 9.4-1206, Thanks for the info
-
a_horse_with_no_name almost 5 years"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