Spring Boot FlywayException: Unable to connect to the database. Configure the url, user and password

Viewed 18232

When I run maven flyway:migrate, I get the error

Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!

I have my Spring Boot settings in my application.yml file, but I guess the error means it doesn't detect the database config. This documention says, "Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup." If I add the configuration to my pom.xml in the flyway plugin section, it connects to the database successfully, but I want it to use my application.yml config. Not the pom.xml. So what am I doing wrong?

Link to repo with issue: https://github.com/jack-cole/BrokenSpringBoot

application.yml

spring:
    datasource:
        driverClassName: org.postgresql.Driver
        url: "jdbc:postgresql://localhost:5433/myDB"
        username: postgres
        password: test123

Dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.16</version>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>7.0.0</version>
</dependency>

Plugins:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>6.5.5</version>
</plugin>
5 Answers

Run With maven:

Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!

You can configure url, user and password in flyway-maven-plugin configuration see First Steps Maven

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>7.0.0</version>
    <configuration>
        <url>jdbc:postgresql://localhost:5433/myDB</url>
        <user>postgres</user>
        <password>test123</password>
    </configuration>
</plugin>

or with environment variables:

mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123

More approaches in https://www.baeldung.com/database-migrations-with-flyway

Run with spring-boot:

Spring Boot auto configure and trigger Flyway at the application startup when you include the Flyway core library into the project. See usage of @ConditionalOnClass(Flyway.class) in FlywayAutoConfiguration :

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Flyway.class)
@Conditional(FlywayDataSourceCondition.class)
@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
      HibernateJpaAutoConfiguration.class })
@Import({ FlywayEntityManagerFactoryDependsOnPostProcessor.class, FlywayJdbcOperationsDependsOnPostProcessor.class,
      FlywayNamedParameterJdbcOperationsDependencyConfiguration.class })
public class FlywayAutoConfiguration {
    ...
}

Use mvn spring-boot:run or java -jar app.jar to run the application

NB : Check also that migration scripts are in db/migration otherwise provide the locations with spring.flyway.locations property

Resources:

https://flywaydb.org/documentation/configuration/parameters/

https://flywaydb.org/documentation/getstarted/firststeps/maven/

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-execute-flyway-database-migrations-on-startup

You quoted a part of spring boot docs, but you launched your migration not by spring boot, but as a maven task.

Flyway maven plugin is not aware of spring boot configuration, it only takes the following sources into account: Overriding order

  1. System properties
  2. Environment variables
  3. Custom config files
  4. Maven properties
  5. Plugin configuration section
  6. Credentials from settings.xml
  7. /flyway.conf
  8. Flyway Maven plugin defaults

On my PC, I used the environment variables approach - I have the same environment variables defined for build plugin and for spring boot.

If you want Flyway to be executed when launching your app (with spring-boot), just remove the flyway-maven-plugin plugin, you don't need it.

-- EDIT

Btw, if you run maven flyway:migrate, you will indeed need to have the credentials set on the plugin (it isn't accessing the java resources).

  • So either you need to run this task => you have to set the credentials on in the plugin on pom.xml.
  • Or you don't need it, so you just remove the plugin and set the credentials on the application.yaml file for your application startup

Have you tried?

application.yml

spring:
    flyway:
        driverClassName: org.postgresql.Driver
        url: "jdbc:postgresql://localhost:5433/myDB"
        user: postgres
        password: test123
        enabled: true

Your configuration has a few missed moments:

  • you use too many spaces for Spring Boot's .yml configuration file. Every next level should contain 2 more spaces, like:
spring:
  datasource:
    url: jdbc:postgresql://localhost/myDB
    username: postgres
    password: root

No more. Otherwise, it would not work!

If you using IntelliJ Idea you could use autoformat shortcut for formatting .yml: Ctrl + Alt + L.

Or you could use the same style as for .properties:

spring.datasource.url: ...
spring.datasource.username: ...

More redundant. However, less chance to make it incorrect.

  • you are using Spring Boot. Thus, you don't need to include the version directly.
    Much better will be left to Spring's dependency management:
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

And

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
</plugin>

For most cases, you could be sure that such a combination of versions will work fine.

  • you should put your migration file under resources/db/migration. Also naming is very important. The first file should be something like:

V1__Init_Db.sql

By default exactly two underscores after V1 -> V1__ !

Here more info: Migrations information.

  • Also, check if you need additional values for your config file, like:
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: create
Related