I\'m building a Spring Boot app, backed by Postgres, using Flyway for database migrations. I\'ve been bumping up against issues where I cannot produce a migration that gener
The configuration below works well with Spring Boot 2.0.
The advantage over embedded-database-spring-test is that this solution doesn't push Flyway into the classpath, possibly messing up Spring Boot's autoconfiguration.
@Configuration
@Slf4j
public class EmbeddedPostgresConfiguration {
@Bean(destroyMethod = "stop")
public PostgresProcess postgresProcess() throws IOException {
log.info("Starting embedded Postgres");
String tempDir = System.getProperty("java.io.tmpdir");
String dataDir = tempDir + "/database_for_tests";
String binariesDir = System.getProperty("java.io.tmpdir") + "/postgres_binaries";
PostgresConfig postgresConfig = new PostgresConfig(
Version.V10_3,
new AbstractPostgresConfig.Net("localhost", Network.getFreeServerPort()),
new AbstractPostgresConfig.Storage("database_for_tests", dataDir),
new AbstractPostgresConfig.Timeout(60_000),
new AbstractPostgresConfig.Credentials("bob", "ninja")
);
PostgresStarter runtime =
PostgresStarter.getInstance(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(binariesDir)));
PostgresExecutable exec = runtime.prepare(postgresConfig);
PostgresProcess process = exec.start();
return process;
}
@Bean(destroyMethod = "close")
@DependsOn("postgresProcess")
DataSource dataSource(PostgresProcess postgresProcess) {
PostgresConfig postgresConfig = postgresProcess.getConfig();
val config = new HikariConfig();
config.setUsername(postgresConfig.credentials().username());
config.setPassword(postgresConfig.credentials().password());
config.setJdbcUrl("jdbc:postgresql://localhost:" + postgresConfig.net().port() + "/" + postgresConfig.storage().dbName());
return new HikariDataSource(config);
}
}
Maven:
ru.yandex.qatools.embed
postgresql-embedded
2.9
test
org.postgresql
postgresql
The class is based on the code I found here: https://github.com/nkoder/postgresql-embedded-example
I modified it to use HikariDatasource
(Spring Boot's default) for proper connection pooling. The binariesDir
and dataDir
are used to avoid costly extraction+initdb in repeated tests.