NACOS适配Postgresql

适配

版本信息

名称 版本
nacos-server 2.4.3
java 1.8.0_212
mvn 3.9.11
postgresql 17.4

下载postgresql插件代码

git clone https://github.com/nacos-group/nacos-plugin.git

编译插件

cd nacos-plugin-2.2.x\nacos-datasource-plugin-ext
mvn clean -DskipTests
mvn install -DskipTests

将编译好的jar包复制到nacos/plugins目录下

cp nacos-plugin-2.2.x\nacos-datasource-plugin-ext\nacos-postgresql-datasource-plugin-ext\target\nacos-postgresql-datasource-plugin-ext-1.0.0-SNAPSHOT.jar nacos/plugins

修改nacos/conf/application.properties文件

spring.datasource.platform=postgresql
db.num=1
db.url.0=jdbc:postgresql://192.168.19.129:5432/nacos?tcpKeepAlive=true&reWriteBatchedInserts=true&ApplicationName=nacos_java
db.user=postgres
db.password=123456
db.pool.config.driverClassName=org.postgresql.Driver

导入schema.sql到postgresql

nacos-plugin-2.2.x\nacos-datasource-plugin-ext\nacos-postgresql-datasource-plugin-ext\src\main\resources\schema\nacos-pg.sql

启动nacos,如果是单节点需要指定启动模式

./startup.sh -m standalone

构建docker容器镜像

拉取代码切换到v2.4.3分支

git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
git checkout v2.4.3

复制编译好的jar包

cd build
cp nacos-plugin-2.2.x\nacos-datasource-plugin-ext\nacos-postgresql-datasource-plugin-ext\target\nacos-postgresql-datasource-plugin-ext-1.0.0-SNAPSHOT.jar .

Dockerfile中新增

ADD nacos-postgresql-datasource-plugin-ext-1.0.0-SNAPSHOT.jar plugins/nacos-postgresql-datasource-plugin-ext-1.0.0-SNAPSHOT.jar

编辑nacos-docker/build/conf/application.properties文件,修改数据库部分

db.num=${POSTGRESQL_DATABASE_NUM:1}
db.url.0=jdbc:postgresql://${POSTGRESQL_SERVICE_HOST}:${POSTGRESQL_SERVICE_PORT:5432}/${POSTGRESQL_SERVICE_DB_NAME}?${POSTGRESQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.user.0=${POSTGRESQL_SERVICE_USER}
db.password.0=${POSTGRESQL_SERVICE_PASSWORD}
db.pool.config.driverClassName=org.postgresql.Driver

构建容器镜像

docker build --build-arg HTTP_PROXY=http://192.168.19.1:7890 --build-arg HTTPS_PROXY=http://192.168.19.1:7890 -t nacos-server:v2.4.3-postgresql .

目前存在的问题

创建配置错误:发布失败。请检查参数是否正确。
排查方式,查看config-server.log

2025-10-29 10:57:34,374 ERROR An error occurred while executing method [insertOrUpdate].
Parameters: [192.168.19.1, null, ConfigInfo{id=0, dataId='test', group='DEFAULT_GROUP', tenant='', appName='', content='test',md5='098f6bcd4621d373cade4e832627b4f6'}, {type=text, config_tags=}].

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: function now(integer) does not exist

错误原因:Nacos 在向 PostgreSQL 数据库插入或更新配置时,调用了 now(?) 函数,并传入了一个整数参数(比如 now(6)),但 PostgreSQL 不支持 now(integer) 这种语法。
解决方式:postgresql创建 now(int) 兼容函数

CREATE OR REPLACE FUNCTION now(int)
RETURNS TIMESTAMP(3) AS $$
BEGIN
RETURN clock_timestamp();
END;
$$ LANGUAGE plpgsql IMMUTABLE;

部署

docker容器部署

docker run -idt --name nacos \
-p 8848:8848 \
-p 9848:9848 \
-p 8080:8080 \
-e NACOS_AUTH_TOKEN=ruiFNxsU5YYGoXrmGcvjCKSYyLCdH5wlAkEI2eQuGQc= \
-e NACOS_AUTH_IDENTITY_KEY=nacos \
-e NACOS_AUTH_IDENTITY_VALUE=nacos \
-e SPRING_DATASOURCE_PLATFORM=postgresql \
-e POSTGRESQL_SERVICE_HOST=192.168.19.129 \
-e POSTGRESQL_SERVICE_PORT=5432 \
-e POSTGRESQL_SERVICE_DB_NAME=nacos \
-e POSTGRESQL_SERVICE_USER=postgres \
-e POSTGRESQL_SERVICE_PASSWORD=123456 \
nacos-server:v2.4.3-postgresql
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/NACOS适配Postgresql/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏