使用 liquibase 创建 Postgres 数据库

Postgres database creation with liquibase

我正在尝试使用 liqubase. I use this 方法创建一个空数据库,但问题是它对我不起作用。

我使用 Postgresql 10,这里有我对 maven 和 liqubase 的配置:

 <plugin>
          <groupId>org.liquibase</groupId>
          <artifactId>liquibase-maven-plugin</artifactId>
          <version>3.0.5</version>
          <configuration>
             <propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile>
          </configuration>
          <executions>
              <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>update</goal>
                    </goals>
              </execution>
          </executions>
  </plugin>

还有我的liqubase.properties

changeLogFile=src/main/resources/liquibase/db.changelog.xml
driver=org.postgresql.Driver
dropFirst=false
url=jdbc:postgresql://localhost:5432/auth?createDatabaseIfNotExist=true
username=postgres
password=root

mvn clean package 上的错误是:

org.postgresql.util.PSQLException: FATAL: database "auth" does not exist

Liquibase 不会创建根本不存在的数据库。我还想象链接 question/answer 中引用的 url 参数 ?createDatabaseIfNotExist=true 可能是 MySql 特定的。

这稍微超出了您的问题范围,但您可以对 Docker postgres instance 使用 liquibase,并根据 docker-library 关于环境变量的文档,将 POSTGRES_DB 设置为"auth"(在你的情况下),它将在 docker 图像启动时创建 "auth" 数据库,然后 liquibase 可以与之交互。