NodeJS+Liquibase操作PostgreSQL数据库未生效求助
问题排查:NodeJS + Liquibase 更新PostgreSQL数据库失败
已完成的操作
1. 本地PostgreSQL数据库创建
CREATE DATABASE node_liquibase_testing; CREATE USER dbmigration WITH ENCRYPTED PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE node_liquibase_testing TO dbmigration;
2. 项目文件内容
changelog.xml
<?xml version="1.1" encoding="UTF-8" standalone="no"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.0.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd"> <changeSet author="sam" id="158"> <createTable tableName="test"> <column name="id" type="INTEGER"></column> <column name="name" type="VARCHAR(255)"/> </createTable> </changeSet> </databaseChangeLog>
test.js
const LiquibaseTS = require('node-liquibase').Liquibase; const POSTGRESQL_DEFAULT_CONFIG = require('node-liquibase').POSTGRESQL_DEFAULT_CONFIG; const myConfig = { ...POSTGRESQL_DEFAULT_CONFIG, changeLogFile: './changelog.xml', url: 'jdbc:postgresql://localhost:5432/node_liquibase_testing', username: 'dbmigration', password: 'password', } const instTs = new LiquibaseTS(myConfig); instTs.status();
3. 执行步骤及结果
执行命令:
npm i node-liquibase node testing.js
终端输出:
Starting Liquibase at 10:02:30 (version 4.1.1 #10 built at 2020-10-12 19:24+0000) 1 change sets have not been applied to dbmigration@jdbc:postgresql://localhost:5432/node_liquibase_testing Liquibase command 'status' was executed successfully.
核心问题:数据库中未生成预期的test表。
排查思路
- 执行正确的Liquibase命令:当前调用的
status()仅用于检查变更集状态,不会执行变更。需改用update()方法应用变更集,修改test.js代码:// 替换原有的instTs.status(); instTs.update().then(() => { console.log('变更集执行完成'); }).catch(err => { console.error('执行失败:', err); }); - 验证数据库Schema权限:确认
dbmigration用户对publicschema有建表权限,可执行补充授权:GRANT ALL ON SCHEMA public TO dbmigration; - 指定数据库Schema:在JDBC URL中明确指定默认Schema,避免路径问题:
jdbc:postgresql://localhost:5432/node_liquibase_testing?currentSchema=public - 启用详细日志:在配置中添加
logLevel: 'debug',查看执行过程中的细节错误。 - 校验changelog文件路径:确保
./changelog.xml相对路径正确,或改用绝对路径避免路径解析问题。 - 检查版本兼容性:确认Liquibase 4.1.1与本地PostgreSQL版本兼容,若PostgreSQL版本过高,考虑升级Liquibase版本。
内容的提问来源于stack exchange,提问作者Vasudevan Sivalingam Pillai
相关产品推荐
相关产品推荐

