Home  >  Article  >  Java  >  How to Specify a Schema When Connecting to PostgreSQL using JDBC?

How to Specify a Schema When Connecting to PostgreSQL using JDBC?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-12 20:55:02456browse

How to Specify a Schema When Connecting to PostgreSQL using JDBC?

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:

  • setParameter(): Set the "currentSchema" parameter on the Connection object.
connection.setParameter("currentSchema", "myschema");
  • setSearchPath(): Set the schema search path directly in the JDBC URL.
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn