Setting Schema on JDBC Connection to PostgreSQL
Can you specify the schema when connecting to Postgres using JDBC?
URL Configuration:
Yes, you can specify the schema in the connection URL. Use the currentSchema parameter to set the desired schema.
Example:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
Compatibility:
This feature is available in JDBC version 9.4 and higher. If you encounter compatibility issues, you can use legacy methods:
Legacy Methods:
connection.setParameter("currentSchema", "myschema");
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
Note:
The patch that introduced the currentSchema parameter proposed the following syntax as well:
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
However, the JDBC URL format with the currentSchema parameter is the recommended approach.
The above is the detailed content of How to Specify a Schema When Connecting to PostgreSQL using JDBC?. For more information, please follow other related articles on the PHP Chinese website!