Backend Development 3 min read

How to Add a Custom PostgreSQL Plugin to Nacos 2.2+ via SPI

Starting with Nacos 2.2.0, you can inject multiple data source plugins via the SPI mechanism; this guide walks through adding a PostgreSQL plugin, importing the required Maven dependencies, loading the database script, configuring connection details, and setting the datasource platform in application.properties.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
How to Add a Custom PostgreSQL Plugin to Nacos 2.2+ via SPI

Nacos from version 2.2.0 can inject multiple data sources via the SPI mechanism; after adding the corresponding data source implementation, you can select the plugin to load at startup by setting the

spring.datasource.platform

property in

application.properties

.

Nacos officially provides default implementations for MySQL and Derby; other database types require custom extensions as described below.

Custom PostgreSQL Plugin

1. Add PostgreSQL plugin

The dependency has been uploaded to Maven Central; do not use the Alibaba Cloud proxy.
<code>&lt;dependency&gt;
  &lt;groupId&gt;com.pig4cloud.plugin&lt;/groupId&gt;
  &lt;artifactId&gt;nacos-datasource-plugin-pg&lt;/artifactId&gt;
  &lt;version&gt;0.0.1&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
  &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

2. Import Nacos PostgreSQL database script

Click to download Nacos PostgreSQL database script [1]

3. Configure Nacos datasource connection information

<code>db:
  num: 1
  url:
    0: jdbc:postgresql://172.27.0.5:5432/pigxx_config
  user:
    0: postgres
  password:
    0: 123456
  pool:
    config:
      driver-class-name: org.postgresql.Driver</code>

4. Specify Nacos datasource platform

<code>spring:
  datasource:
    platform: postgresql</code>

Reference Materials

[1] Click to download Nacos PostgreSQL: https://minio.pigx.vip/oss/202212/1671184224.sql

PluginNacosPostgreSQLSPIDataSource
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.