问题描述
我正在尝试使用liquibase在PostgreSQL上创建数据库。这是我的dbchangelog文件
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="testuser" id="3">
<sql dbms="postgresql"
endDelimiter=";">
CREATE DATABASE testdb;
</sql>
</changeSet>
</databaseChangeLog
当我尝试从我的项目目录运行liquibase update
时
Unexpected error running Liquibase: ERROR: CREATE DATABASE cannot run inside a transaction block [Failed SQL: (0) CREATE DATABASE testdb]
我看到在PostgreSQL中设置AUTOCOMMIT = ON可以解决此问题,但是此方法仅适用于PostgreSQL 9.4及更低版本。但是我正在使用AWS RDS PostgreSQL 9.6.8
解决方法
简短:
将 runInTransaction="false"
添加到您的 changeSet
。
说明:
Liquibase 打开一个事务块,以保证 changeSet
完全执行或根本不执行。
PostgreSQL 文档说:CREATE DATABASE cannot be executed inside a transaction block.
(见 PostgreSQL Doc on Create Database!)
因此,显然存在冲突行为,只能通过不使用事务块来避免。
请注意,使用多个命令停用事务行为,可能会导致更改集仅部分执行的问题。
,Liquibase不适用于数据库创建。您应该已经准备好数据库模式,并且liquibase将用表/索引/ ...填充它。