How To Auto-Create And Migrate Two Schemas In PostgreSQL Using Flyway
Note: For production, don't rely on hibernate.ddl-auto
(or counterparts) to export schema DDL to the database. Simply remove (disable) hibernate.ddl-auto
or set it to validate
. Rely on Flyway or Liquibase.
Description: This application is an example of auto-creating and migrating two schemas in PostgreSQL using Flyway. In addition, each data source uses its own HikariCP connection pool. In case of PostgreSQL, where a database can have multiple schemas, we use the default postgres
database and auto-create two schemas, authors
and books
. For this we rely on Flyway, which is capable to create the missing schemas.
Key points:
- for Maven, in
pom.xml
, add the Flyway dependency - remove (disable)
spring.jpa.hibernate.ddl-auto
or set it tovalidate
- in
application.properties
, configure the JDBC URL forbooks
asjdbc:postgresql://localhost:5432/postgres?currentSchema=books
and forauthors
asjdbc:postgresql://localhost:5432/postgres?currentSchema=authors
- in
application.properties
, setspring.flyway.enabled=false
to disable default behavior - programmatically create two
DataSource
, one forbooks
and one forauthors
- programmatically create two
FlywayDataSource
, one forbooks
and one forauthors
- programmatically create two
EntityManagerFactory
, one forbooks
and one forauthors
- for
books
, place the migration SQLs files indb\migration\books
- for
authors
, place the migration SQLs files indb\migration\authors