Postgres JDBC 连接为应用程序用户而不是系统用户

Postgres JDBC connection as app user instead of system user

我正在使用 flyway 迁移我的集成测试数据库作为我的 maven 构建的一部分。

我在 pre-integration-test 阶段将 flyway 配置为 运行。它每次都会清理然后重建数据库。

<build>
  <plugins>

    <!-- Flyway -->
    <plugin>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-maven-plugin</artifactId>
      <version>5.1.4</version>

      <configuration>
        <url>${jdbc.url}</url>
        <schemas>
          <schema>public</schema>
        </schemas>
      </configuration>
      <executions>
        <execution>
          <id>migrate-database</id>
          <phase>pre-integration-test</phase>
          <goals>
            <goal>clean</goal>
            <goal>migrate</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

  </plugins>
</build>

我 运行 使用 mvn clean install 构建,将 JDBC url 传递给测试数据库。如您所见,它以 postgres 用户 racer

的身份连接到 racer_test
mvn -Djdbc.url=jdbc:postgresql://localhost:5432/racer_test?user%3Dracer%26password%3Dfoo clean install

构建期间一切运行都很好

[INFO] --- flyway-maven-plugin:5.1.4:clean (migrate-database) @ racer-api ---
[INFO] Flyway Community Edition 5.1.4 by Boxfuse
[INFO] Database: jdbc:postgresql://localhost:5432/racer_test (PostgreSQL 9.6)
[INFO] Successfully cleaned schema "public" (execution time 00:00.026s)
[INFO]
[INFO] --- flyway-maven-plugin:5.1.4:migrate (migrate-database) @ racer-api ---
[INFO] Database: jdbc:postgresql://localhost:5432/racer_test (PostgreSQL 9.6)
[INFO] Successfully validated 1 migration (execution time 00:00.005s)
[INFO] Creating Schema History table: "public"."flyway_schema_history"
[INFO] Current version of schema "public": << Empty Schema >>
[INFO] Migrating schema "public" to version 1 - create users
[INFO] Successfully applied 1 migration to schema "public" (execution time 00:00.040s)

然而,当我在 postgres 中查看数据库时,它创建了表和其他对象作为我登录的系统用户 (jeeves) 而不是我指定的用户 (racer)

racer_test=> \d
                  List of relations
 Schema |         Name          |   Type   | Owner
--------+-----------------------+----------+--------
 public | flyway_schema_history | table    | jeeves
 public | users                 | table    | jeeves
 public | users_id_seq          | sequence | jeeves
(3 rows)

我以某种方式指定用户是否不正确?它不应该是 JDBC url 的一部分吗?

谢谢!

您应该使用 flyway 用户参数来执行此操作:只需将 -Dflyway.user=racer 添加到您的 maven 命令中,它应该可以正常工作!如果您使用的是 flyway.properties 文件,只需在其后附加 flyway.user=racer

此外,如果使用 url,使用 postgres 的用户应该设置如下:

jdbc:postgresql://{$user}:{$optional_password}@{$host}:{$port}/{$db_name}{$GET_parameters}