【问题标题】:Is it possible to specify the schema when connecting to postgres with JDBC?使用 JDBC 连接到 postgres 时是否可以指定模式?
【发布时间】:2011-05-09 07:05:22
【问题描述】:

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

【问题讨论】:

    标签: java database postgresql jdbc database-schema


    【解决方案1】:

    我不相信有办法在连接字符串中指定架构。看来你必须执行

    set search_path to 'schema'
    

    建立连接后指定架构。

    【讨论】:

    • 这对我有用,特别是使用“连接”实例来运行:Statement statement = connection.createStatement(); try { statement.execute("set search_path to '" + schema + "'"); } finally { statement.close(); }
    • 有一种方法可以在连接字符串(jdbc uri)中指定默认模式。请参阅下面的答案。
    【解决方案2】:

    如果在您的环境中可能,您还可以将用户的默认架构设置为您想要的架构:

    ALTER USER user_name SET search_path to 'schema'
    

    【讨论】:

    • 可能更好地改变数据库本身,以便同一个用户可以根据需要连接到具有不同搜索路径的不同数据库:ALTER DATABASE dbname SET search_path TO public,schemaname;
    【解决方案3】:

    我知道这已经得到解答,但我在尝试指定用于 liquibase 命令行的架构时遇到了同样的问题。

    更新 从 JDBC v9.4 开始,您可以使用新的 currentSchema 参数指定 url,如下所示:

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

    基于较早的补丁出现:

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

    建议的网址是这样的:

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

    【讨论】:

    • 是的,但在撰写本文时(2012 年末),它不是9.1 driver 的一部分,请参阅:Connection Parameters
    • 你试过了吗?因为它没有被列为以前驱动程序的一部分,但它仍然有效。
    • 试过 9.3-1101-jdbc41 和 9.1,对我不起作用
    • @IgnacioA.Poletti 在创建连接后尝试使用 JDCB setSchema 方法。使用最近的 postgres 驱动程序为我工作。
    • 我们通过使用不同的(更新的)JDBC 驱动程序解决了这个问题。在我们的例子中,postgresql-9.4.1209.jdbc42.jar9.5 数据库和 ?currentSchema=myschema 语法一起使用。
    【解决方案4】:

    几年前,我向 PostgreSQL JDBC 驱动程序提交了一个更新版本的补丁来启用它。您必须从源代码构建 PostreSQL JDBC 驱动程序(添加补丁后)才能使用它:

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

    http://jdbc.postgresql.org/

    【讨论】:

      【解决方案5】:

      不要忘记SET SCHEMA 'myschema',您可以在单独的声明中使用它

      SET SCHEMA 'value' 是 SET search_path TO value 的别名。只有一个 可以使用此语法指定架构。

      自 9.4 及可能更早的 JDBC 驱动程序版本开始,支持 setSchema(String schemaName) 方法。

      【讨论】:

        【解决方案6】:

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

        例如:

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

        【讨论】:

          【解决方案7】:

          DataSourcesetCurrentSchema

          在实例化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 节The Public Schema。引用帽子手册:

          在前面的部分中,我们创建了表而不指定任何模式名称。默认情况下,此类表(和其他对象)会自动放入名为“public”的模式中。每个新数据库都包含这样的模式。

          【讨论】:

          • 尝试连接到模式” - 这有点误导。驱动程序不连接到“模式”,而是连接到数据库。查询使用哪种模式取决于search_path 的当前设置
          【解决方案8】:

          在 Go 中使用“sql.DB”(注意 search_path 带有下划线):

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-02
            • 2016-08-21
            • 2021-07-10
            • 2017-09-08
            相关资源
            最近更新 更多