Search code examples
javapostgresqljdbcflyway

Postgres JDBC connection as app user instead of system user


I'm using flyway to migrate my integration test DB as part of my maven build.

I have flyway configured to run during the pre-integration-test phase. It cleans and then rebuilds the DB each time.

<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>

I run the build with mvn clean install, passing it a JDBC url for the test DB. As you can see it connects to racer_test as the postgres user racer

mvn -Djdbc.url=jdbc:postgresql://localhost:5432/racer_test?user%3Dracer%26password%3Dfoo clean install

Everything runs fine during the build

[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)

However when I look at the database in postgres, it created the tables and other objects as my system user that I'm logged in as (jeeves) and not the user that I specified (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)

Am I incorrect in specifying the user somehow? Should it not be part of the JDBC url?

Thanks!


Solution

  • You should use flyway user parameter to do so: just add -Dflyway.user=racer to your maven command, that should work correctly! If you're using a flyway.properties file, just append flyway.user=racer to it.

    Additionally users with postgres are supposed to be set as follows if using the url :

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