{"id":136932,"date":"2025-09-04T18:36:00","date_gmt":"2025-09-04T15:36:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=136932"},"modified":"2025-09-04T12:55:26","modified_gmt":"2025-09-04T09:55:26","slug":"spring-boot-flyway-manage-multiple-databases","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html","title":{"rendered":"Spring Boot &amp; Flyway: Manage Multiple Databases"},"content":{"rendered":"<p>Managing multiple databases in a Spring Boot application can be challenging, especially when you want to maintain database migrations consistently. Flyway provides an elegant solution to version control your database schemas and ensure smooth migrations. Let us delve into understanding how we can use Flyway &amp; Spring Boot to migrate multiple databases and how they can be efficiently managed in an application.<\/p>\n<h2><a name=\"section-1\"><\/a>1. What is Flyway?<\/h2>\n<p>Flyway is an open-source database migration tool that helps manage and version control schema changes in a consistent and automated way. It supports multiple databases, including <a href=\"https:\/\/www.postgresql.org\/\" target=\"_blank\" rel=\"noopener\">PostgreSQL<\/a>, <a href=\"https:\/\/www.h2database.com\/\" target=\"_blank\" rel=\"noopener\">H2<\/a>, <a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\">MySQL<\/a>, and many others. Flyway uses a simple folder-based structure where SQL scripts are stored in a <code>db\/migration<\/code> directory. Each script follows a naming convention like <code>V1__Initial_Setup.sql<\/code>, <code>V2__Add_Column.sql<\/code>, etc., ensuring that changes are applied in order.<\/p>\n<p>In a <a href=\"https:\/\/spring.io\/projects\/spring-boot\" target=\"_blank\" rel=\"noopener\">Spring Boot<\/a> application, Flyway integrates seamlessly to run database migrations automatically during application startup. This eliminates manual intervention, reduces errors, and ensures that all environments development, staging, production) stay synchronized with the latest database schema changes.<\/p>\n<h2><a name=\"section-2\"><\/a>2. Setting up Database<\/h2>\n<p>This section demonstrates how to configure a PostgreSQL container using Docker Compose. Feel free to change the database name, username, and password values as per your environment or security requirements.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">version: '3.8'\nservices:\n  postgres-secondary:\n    image: postgres:15\n    container_name: postgres-secondary\n    environment:\n      POSTGRES_DB: secondarydb\n      POSTGRES_USER: admin\n      POSTGRES_PASSWORD: password\n    ports:\n      - \"5432:5432\"\n    volumes:\n      - postgres_secondary_data:\/var\/lib\/postgresql\/data\n\nvolumes:\n  postgres_secondary_data:\n<\/pre>\n<p>The above Docker Compose configuration defines a PostgreSQL service named <code>postgres-secondary<\/code> using the official <code>postgres:15<\/code> image. It sets up environment variables for database name, username, and password, maps the container&#8217;s PostgreSQL port 5432 to the host, and attaches a named volume <code>postgres_secondary_data<\/code> to persist data across container restarts. Ensure that you modify sensitive values like <code>POSTGRES_PASSWORD<\/code> to meet your security guidelines.<\/p>\n<h2><a name=\"section-3\"><\/a>3. Code Example<\/h2>\n<h3>3.1 Maven Configuration (pom.xml)<\/h3>\n<p>This section adds essential Spring Boot, Flyway, and database dependencies to the <code>pom.xml<\/code> file.<\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.flywaydb&lt;\/groupId&gt;\n    &lt;artifactId&gt;flyway-core&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.flywaydb&lt;\/groupId&gt;\n    &lt;artifactId&gt;flyway-database-postgresql&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.postgresql&lt;\/groupId&gt;\n    &lt;artifactId&gt;postgresql&lt;\/artifactId&gt;\n    &lt;scope&gt;runtime&lt;\/scope&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.h2database&lt;\/groupId&gt;\n    &lt;artifactId&gt;h2&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>The above configuration includes dependencies for Spring Boot&#8217;s JPA and Web modules, Flyway for database migrations, PostgreSQL as the primary database driver, and H2 for in-memory database testing. These dependencies enable seamless database setup and migration handling in a Spring Boot application.<\/p>\n<h3>3.2 Configuration for Multiple Databases<\/h3>\n<p>This section provides the basic application properties required for working with multiple databases in a Spring Boot project.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">spring.application.name=demo-multipledatabases\n\n# JPA Configuration\nspring.jpa.hibernate.ddl-auto=none\nspring.jpa.show-sql=true\n\n# H2 Console (for development\/debugging)\nspring.h2.console.enabled=true\nspring.h2.console.path=\/h2-console\n<\/pre>\n<p>The above configuration sets the Spring Boot application name, disables automatic schema generation (<code>ddl-auto=none<\/code>), and enables SQL statement logging. It also activates the H2 database console for debugging and sets its access path to <code>\/h2-console<\/code>, which is useful when working with an in-memory database during development.<\/p>\n<h3>3.3 Datasource Configuration Classes<\/h3>\n<h4>3.3.1 Creating Primary Database Configuration<\/h4>\n<p>This section defines the configuration for connecting Spring Boot to the primary database using H2 and Flyway for migrations. Feel free to update database URL, username, and password as per your environment needs.<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.demo.config;\n\nimport com.zaxxer.hikari.HikariDataSource;\nimport jakarta.persistence.EntityManagerFactory;\nimport org.flywaydb.core.Flyway;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.Primary;\nimport org.springframework.data.jpa.repository.config.EnableJpaRepositories;\nimport org.springframework.orm.jpa.JpaTransactionManager;\nimport org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;\nimport org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;\nimport org.springframework.transaction.PlatformTransactionManager;\nimport org.springframework.transaction.annotation.EnableTransactionManagement;\n\nimport javax.sql.DataSource;\nimport java.util.HashMap;\nimport java.util.Map;\n\n@Configuration\n@EnableTransactionManagement\n@EnableJpaRepositories(\n        entityManagerFactoryRef = \"primaryEntityManagerFactory\",\n        transactionManagerRef = \"primaryTransactionManager\",\n        basePackages = {\"com.demo.repository.primary\"}\n)\npublic class PrimaryDatabaseConfig {\n\n    @Primary\n    @Bean(name = \"primaryDataSource\")\n    public DataSource primaryDataSource() {\n        HikariDataSource dataSource = new HikariDataSource();\n        dataSource.setJdbcUrl(\"jdbc:h2:mem:primarydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE\");\n        dataSource.setUsername(\"sa\");\n        dataSource.setPassword(\"\");\n        dataSource.setDriverClassName(\"org.h2.Driver\");\n        return dataSource;\n    }\n\n    @Primary\n    @Bean(name = \"primaryEntityManagerFactory\")\n    public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(\n            @Qualifier(\"primaryDataSource\") DataSource dataSource) {\n\n        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();\n        em.setDataSource(dataSource);\n        em.setPackagesToScan(\"com.demo.entity.primary\");\n        em.setPersistenceUnitName(\"primary\");\n\n        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();\n        em.setJpaVendorAdapter(vendorAdapter);\n\n        Map&lt;String, Object&gt; properties = new HashMap&lt;&gt;();\n        properties.put(\"hibernate.dialect\", \"org.hibernate.dialect.H2Dialect\");\n        properties.put(\"hibernate.hbm2ddl.auto\", \"none\");\n        properties.put(\"hibernate.show_sql\", true);\n        em.setJpaPropertyMap(properties);\n\n        return em;\n    }\n\n    @Primary\n    @Bean(name = \"primaryTransactionManager\")\n    public PlatformTransactionManager primaryTransactionManager(\n            @Qualifier(\"primaryEntityManagerFactory\") EntityManagerFactory primaryEntityManagerFactory) {\n        return new JpaTransactionManager(primaryEntityManagerFactory);\n    }\n\n    @Primary\n    @Bean(name = \"primaryFlyway\")\n    public Flyway primaryFlyway(@Qualifier(\"primaryDataSource\") DataSource dataSource) {\n        return Flyway.configure()\n                .dataSource(dataSource)\n                .locations(\"classpath:db\/migration\/primary\")\n                .baselineOnMigrate(true)\n                .load();\n    }\n}\n<\/pre>\n<p>This configuration class defines beans for the primary database connection using HikariCP, sets up the entity manager and transaction manager for handling JPA operations, and configures Flyway to manage database migrations from the <code>classpath:db\/migration\/primary<\/code> location. The use of <code>@Primary<\/code> ensures that these beans take precedence when multiple data sources are present. Update database connection properties if needed for different environments.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h4>3.3.2 Creating Secondary Database Configuration<\/h4>\n<p>This section sets up the secondary database connection using PostgreSQL and Flyway for schema migrations. Ensure that the username, password, and database URL are updated to match your environment configuration.<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.demo.config;\n\nimport com.zaxxer.hikari.HikariDataSource;\nimport jakarta.persistence.EntityManagerFactory;\nimport org.flywaydb.core.Flyway;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.jpa.repository.config.EnableJpaRepositories;\nimport org.springframework.orm.jpa.JpaTransactionManager;\nimport org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;\nimport org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;\nimport org.springframework.transaction.PlatformTransactionManager;\nimport org.springframework.transaction.annotation.EnableTransactionManagement;\n\nimport javax.sql.DataSource;\nimport java.util.HashMap;\nimport java.util.Map;\n\n@Configuration\n@EnableTransactionManagement\n@EnableJpaRepositories(\n        entityManagerFactoryRef = \"secondaryEntityManagerFactory\",\n        transactionManagerRef = \"secondaryTransactionManager\",\n        basePackages = {\"com.demo.repository.secondary\"}\n)\npublic class SecondaryDatabaseConfig {\n\n    @Bean(name = \"secondaryDataSource\")\n    public DataSource secondaryDataSource() {\n        HikariDataSource dataSource = new HikariDataSource();\n        dataSource.setJdbcUrl(\"jdbc:postgresql:\/\/localhost:5432\/secondarydb\");\n        dataSource.setUsername(\"postgres\");\n        dataSource.setPassword(\"postgres\");\n        dataSource.setDriverClassName(\"org.postgresql.Driver\");\n        return dataSource;\n    }\n\n    @Bean(name = \"secondaryEntityManagerFactory\")\n    public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(\n            @Qualifier(\"secondaryDataSource\") DataSource dataSource) {\n\n        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();\n        em.setDataSource(dataSource);\n        em.setPackagesToScan(\"com.demo.entity.secondary\");\n        em.setPersistenceUnitName(\"secondary\");\n\n        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();\n        em.setJpaVendorAdapter(vendorAdapter);\n\n        Map&lt;String, Object&gt; properties = new HashMap&lt;&gt;();\n        properties.put(\"hibernate.dialect\", \"org.hibernate.dialect.PostgreSQLDialect\");\n        properties.put(\"hibernate.hbm2ddl.auto\", \"none\");\n        properties.put(\"hibernate.show_sql\", true);\n        em.setJpaPropertyMap(properties);\n\n        return em;\n    }\n\n    @Bean(name = \"secondaryTransactionManager\")\n    public PlatformTransactionManager secondaryTransactionManager(\n            @Qualifier(\"secondaryEntityManagerFactory\") EntityManagerFactory secondaryEntityManagerFactory) {\n        return new JpaTransactionManager(secondaryEntityManagerFactory);\n    }\n\n    @Bean(name = \"secondaryFlyway\")\n    public Flyway secondaryFlyway(@Qualifier(\"secondaryDataSource\") DataSource dataSource) {\n        return Flyway.configure()\n                .dataSource(dataSource)\n                .locations(\"classpath:db\/migration\/secondary\")\n                .baselineOnMigrate(true)\n                .load();\n    }\n}\n<\/pre>\n<p>This configuration connects the application to a PostgreSQL database for secondary operations, defines a separate <code>EntityManagerFactory<\/code> and <code>TransactionManager<\/code>, and uses Flyway to manage schema migrations from <code>classpath:db\/migration\/secondary<\/code>. It ensures isolation of JPA operations between primary and secondary data sources.<\/p>\n<h4>3.3.3 Creating Migration Runner<\/h4>\n<p>This section defines a runner that triggers Flyway migrations for both primary and secondary databases during application startup.<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.demo.config;\n\nimport org.flywaydb.core.Flyway;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.boot.CommandLineRunner;\nimport org.springframework.core.annotation.Order;\nimport org.springframework.stereotype.Component;\n\n@Component\n@Order(1) \/\/ Run before DataInitializer\npublic class FlywayMigrationRunner implements CommandLineRunner {\n\n    private final Flyway primaryFlyway;\n    private final Flyway secondaryFlyway;\n\n    public FlywayMigrationRunner(@Qualifier(\"primaryFlyway\") Flyway primaryFlyway,\n                                 @Qualifier(\"secondaryFlyway\") Flyway secondaryFlyway) {\n        this.primaryFlyway = primaryFlyway;\n        this.secondaryFlyway = secondaryFlyway;\n    }\n\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(\"Running database migrations...\");\n\n        System.out.println(\"Migrating primary database...\");\n        primaryFlyway.migrate();\n\n        System.out.println(\"Migrating secondary database...\");\n        secondaryFlyway.migrate();\n\n        System.out.println(\"Database migrations completed successfully!\");\n    }\n}\n<\/pre>\n<p>This class implements <code>CommandLineRunner<\/code> to execute Flyway migrations when the application starts. It runs both the primary and secondary database migrations in sequence and ensures that schema updates are applied before other initialization tasks. The <code>@Order(1)<\/code> annotation guarantees that this runner executes early in the application startup process.<\/p>\n<h3>3.4 Flyway Configuration<\/h3>\n<p>This section contains the SQL migration scripts that Flyway uses to initialize database schemas for both primary and secondary data sources.<\/p>\n<h4>3.4.1 Create Primary Datasource SQL Script<\/h4>\n<p>The following script creates the <code>users<\/code> table for the primary database.<\/p>\n<pre class=\"brush:sql; wrap-lines:false;\">-- src\/main\/resources\/db\/migration\/primary\/V1__Create_users_table.sql\nCREATE TABLE users (\n    id BIGINT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL UNIQUE,\n    email VARCHAR(100) NOT NULL UNIQUE,\n    first_name VARCHAR(50) NOT NULL,\n    last_name VARCHAR(50) NOT NULL,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n<\/pre>\n<p>This migration script defines a <code>users<\/code> table with unique constraints on <code>username<\/code> and <code>email<\/code>, along with timestamps for tracking creation and updates. Flyway automatically executes it when the application starts.<\/p>\n<h4>3.4.2 Create Secondary Datasource SQL Script<\/h4>\n<p>The following script creates the <code>products<\/code> table for the secondary database.<\/p>\n<pre class=\"brush:sql; wrap-lines:false;\">-- src\/main\/resources\/db\/migration\/secondary\/V1__Create_products_table.sql\nCREATE TABLE products (\n    id BIGSERIAL PRIMARY KEY,\n    name VARCHAR(100) NOT NULL,\n    description TEXT,\n    price DECIMAL(10,2) NOT NULL,\n    category VARCHAR(50) NOT NULL,\n    stock_quantity INTEGER DEFAULT 0,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n<\/pre>\n<p>This migration script initializes the <code>products<\/code> table with fields for product details, pricing, stock quantity, and timestamps. Flyway applies it to the secondary database during startup.<\/p>\n<h3>3.5 Code Run and Output<\/h3>\n<p>When you start the Spring Boot application, Flyway will automatically detect the migrations and apply them to the respective databases. You can verify this in the application logs, which will show migration details for both primary and secondary databases.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">2025-09-02T12:38:26.149+05:30  INFO 35473 --- [demo-multipledatabases] [           main] c.demo.DemoMultipledatabasesApplication  : Started DemoMultipledatabasesApplication in 2.047 seconds (process running for 2.366)\nRunning database migrations...\nMigrating primary database...\n2025-09-02T12:38:26.171+05:30  INFO 35473 --- [demo-multipledatabases] [           main] org.flywaydb.core.FlywayExecutor         : Database: jdbc:h2:mem:primarydb (H2 2.3)\n2025-09-02T12:38:26.181+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Schema history table \"PUBLIC\".\"flyway_schema_history\" does not exist yet\n2025-09-02T12:38:26.182+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.005s)\n2025-09-02T12:38:26.185+05:30  INFO 35473 --- [demo-multipledatabases] [           main] org.flywaydb.core.Flyway                 : All configured schemas are empty; baseline operation skipped. A baseline or migration script with a lower version than the baseline version may execute if available. Check the Schemas parameter if this is not intended.\n2025-09-02T12:38:26.185+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table \"PUBLIC\".\"flyway_schema_history\" ...\n2025-09-02T12:38:26.198+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbMigrate      : Current version of schema \"PUBLIC\": &lt;&gt;\n2025-09-02T12:38:26.200+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbMigrate      : Migrating schema \"PUBLIC\" to version \"1 - Create users table\"\n2025-09-02T12:38:26.207+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema \"PUBLIC\", now at version v1 (execution time 00:00.002s)\nMigrating secondary database...\n2025-09-02T12:38:26.212+05:30  INFO 35473 --- [demo-multipledatabases] [           main] org.flywaydb.core.FlywayExecutor         : Database: jdbc:postgresql:\/\/localhost:5432\/secondarydb (PostgreSQL 17.5)\n2025-09-02T12:38:26.233+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.009s)\n2025-09-02T12:38:26.243+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbMigrate      : Current version of schema \"public\": 1\n2025-09-02T12:38:26.244+05:30  INFO 35473 --- [demo-multipledatabases] [           main] o.f.core.internal.command.DbMigrate      : Schema \"public\" is up to date. No migration necessary.\nDatabase migrations completed successfully!\n<\/pre>\n<p>These logs confirm that Flyway detected the migration scripts located under <code>classpath:db\/migration\/primary<\/code> and <code>classpath:db\/migration\/secondary<\/code> and applied them successfully.<\/p>\n<h2><a name=\"section-4\"><\/a>4. Conclusion<\/h2>\n<p>Spring Boot with Flyway makes it simple to manage multiple databases with proper version control for schema migrations. By configuring separate datasources and migration paths, you can maintain independent database schemas, reduce conflicts, and ensure reliable deployments. This setup is especially useful in microservices or applications with modular databases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing multiple databases in a Spring Boot application can be challenging, especially when you want to maintain database migrations consistently. Flyway provides an elegant solution to version control your database schemas and ensure smooth migrations. Let us delve into understanding how we can use Flyway &amp; Spring Boot to migrate multiple databases and how they &hellip;<\/p>\n","protected":false},"author":26931,"featured_media":121875,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[936,2153,964,1185,2794,1152,657,854],"class_list":["post-136932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-docker","tag-docker-compose","tag-flyway","tag-h2","tag-h2-database","tag-postgres","tag-postgresql","tag-spring-boot"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2025-09-04T15:36:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin Batra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin Batra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html\"},\"author\":{\"name\":\"Yatin Batra\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\"},\"headline\":\"Spring Boot &amp; Flyway: Manage Multiple Databases\",\"datePublished\":\"2025-09-04T15:36:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html\"},\"wordCount\":838,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/spring-boot-logo.jpg\",\"keywords\":[\"Docker\",\"Docker Compose\",\"Flyway\",\"H2\",\"H2 Database\",\"Postgres\",\"PostgreSQL\",\"Spring Boot\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html\",\"name\":\"Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/spring-boot-logo.jpg\",\"datePublished\":\"2025-09-04T15:36:00+00:00\",\"description\":\"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/spring-boot-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/spring-boot-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-flyway-manage-multiple-databases.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Boot &amp; Flyway: Manage Multiple Databases\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\",\"name\":\"Yatin Batra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"caption\":\"Yatin Batra\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\\\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yatin-batra\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks","description":"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html","og_locale":"en_US","og_type":"article","og_title":"Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks","og_description":"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.","og_url":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2025-09-04T15:36:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg","type":"image\/jpeg"}],"author":"Yatin Batra","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin Batra","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html"},"author":{"name":"Yatin Batra","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d"},"headline":"Spring Boot &amp; Flyway: Manage Multiple Databases","datePublished":"2025-09-04T15:36:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html"},"wordCount":838,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg","keywords":["Docker","Docker Compose","Flyway","H2","H2 Database","Postgres","PostgreSQL","Spring Boot"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html","url":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html","name":"Spring Boot &amp; Flyway: Manage Multiple Databases - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg","datePublished":"2025-09-04T15:36:00+00:00","description":"Spring boot flyway multiple databases: Manage schema migrations across multiple databases in Spring Boot using Flyway seamlessly.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/spring-boot-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-flyway-manage-multiple-databases.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Boot &amp; Flyway: Manage Multiple Databases"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d","name":"Yatin Batra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","caption":"Yatin Batra"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/www.javacodegeeks.com\/author\/yatin-batra"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/136932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26931"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=136932"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/136932\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/121875"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=136932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=136932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=136932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}