All Products
Search
Document Center

ApsaraDB RDS:Build an AI application based on ApsaraDB RDS for PostgreSQL and the Dify platform

Last Updated:Feb 28, 2026

Dify is an open-source platform for large language model (LLM) application development. It integrates Backend as a Service (BaaS) with LLMOps, enabling rapid development of production-ready Generative AI applications. This tutorial walks through creating an AI Q&A application that uses ApsaraDB RDS for PostgreSQL as both the relational database and the vector store, with Dify as the application platform.

Prerequisites

Before you begin, prepare the following:

  • An Alibaba Cloud account with access to ApsaraDB RDS and Elastic Compute Service (ECS)

  • A corpus file for the knowledge base. Supported formats: TXT, MARKDOWN, MDX, PDF, HTML, XLSX, XLS, DOCX, CSV, MD, and HTM. Maximum file size: 15 MB per file

  • (Optional) An API key from Alibaba Cloud Bailian if you plan to use Tongyi Qianwen as the LLM

Step 1: Set up the ApsaraDB RDS for PostgreSQL instance

  1. Create an ApsaraDB RDS for PostgreSQL instance.

  2. Create an account and a database for the RDS instance. For more information, see Create a database and an account.

    • Set Account Type to Privileged Account.

    • Set Authorized Account to the privileged account you created.

  3. Apply for a public endpoint for the RDS instance. For more information, see Enable or disable a public endpoint.

  4. Add the public IP address of your ECS instance to the whitelist of the RDS instance. For more information, see Set a whitelist.

  5. Enable the vector plug-in (pgvector) for the target database. For more information, see Manage plug-ins.

Step 2: Deploy Dify on ECS

Create an ECS instance

Create an ECS instance. For more information, see Custom purchase of ECS instances.

ECS typeSupported AI models
CPU-basedOnline AI large models only
GPU-basedBoth online and local AI large models
Important
  • This tutorial uses Alibaba Cloud Linux 3 as an example.

  • If you purchase a GPU-based ECS instance, install the corresponding GPU driver when configuring the image. A GPU-based instance lets you deploy large models on ECS through Ollama.

Install Docker

Install Docker on the ECS instance. For more information, see Install Docker.

(Optional) Install NVIDIA Container Toolkit

If you purchased a GPU-based ECS instance, run the following commands to install the NVIDIA Container Toolkit:

curl -s -L https://nvidia.github.io/nvidia-container-runtime/centos8/nvidia-container-runtime.repo | \
sudo tee /etc/yum.repos.d/nvidia-container-runtime.repo

sudo yum install -y nvidia-container-toolkit

# Restart Docker
sudo systemctl restart docker

Get the Dify source code

Clone the Dify repository:

git clone https://github.com/langgenius/dify.git

To install a specific version, use the --branch parameter. For example, to install version v1.0.0:

git clone https://github.com/langgenius/dify.git --branch 1.0.0

For available versions, see Dify tags.

Note

If Git is not installed, run sudo yum install git -y to install it.

Configure ApsaraDB RDS for PostgreSQL as the database and vector store

Set ApsaraDB RDS for PostgreSQL as both the default database and the default vector store for Dify. Choose one of the following configuration methods.

Option 1: Use environment variables

Set ApsaraDB RDS for PostgreSQL as the default database:

export DB_USERNAME=testdbuser
export DB_PASSWORD=dbPassword
export DB_HOST=pgm-****.pg.rds.aliyuncs.com
export DB_PORT=5432
export DB_DATABASE=testdb01

Replace the placeholder values with your actual RDS instance information.

ParameterDescription
DB_USERNAMEThe privileged account of the ApsaraDB RDS for PostgreSQL instance.
DB_PASSWORDThe password of the privileged account.
DB_HOSTThe public endpoint of the ApsaraDB RDS for PostgreSQL instance.
DB_PORTThe public port of the ApsaraDB RDS for PostgreSQL instance. Default: 5432.
DB_DATABASEThe database name in the ApsaraDB RDS for PostgreSQL instance.

Set ApsaraDB RDS for PostgreSQL as the default vector store:

export VECTOR_STORE=pgvector
export PGVECTOR_HOST=pgm-****.pg.rds.aliyuncs.com
export PGVECTOR_PORT=5432
export PGVECTOR_USER=testdbuser
export PGVECTOR_PASSWORD=dbPassword
export PGVECTOR_DATABASE=testdb01

Replace the placeholder values with your actual RDS instance information.

ParameterDescription
VECTOR_STOREThe vector store type. Set to pgvector.
PGVECTOR_USERThe privileged account of the ApsaraDB RDS for PostgreSQL instance.
PGVECTOR_PASSWORDThe password of the privileged account.
PGVECTOR_HOSTThe public endpoint of the ApsaraDB RDS for PostgreSQL instance.
PGVECTOR_PORTThe public port of the ApsaraDB RDS for PostgreSQL instance. Default: 5432.
PGVECTOR_DATABASEThe database name in the ApsaraDB RDS for PostgreSQL instance.

Option 2: Use the .env file

  1. Copy the Dify example configuration file:

    Note

    After cloning the Dify source code, the /root/dify/docker directory automatically contains the .env.example and .env files.

       cd /root/dify/docker
       cp .env.example .env
  2. Edit the .env file and set the following parameters to your ApsaraDB RDS for PostgreSQL instance information:

       # Default database configuration
       DB_USERNAME=testdbuser
       DB_PASSWORD=dbPassword
       DB_HOST=pgm-****.pg.rds.aliyuncs.com
       DB_PORT=5432
       DB_DATABASE=testdb01
    
       # Default vector store configuration
       VECTOR_STORE=pgvector
       PGVECTOR_HOST=pgm-****.pg.rds.aliyuncs.com
       PGVECTOR_PORT=5432
       PGVECTOR_USER=testdbuser
       PGVECTOR_PASSWORD=dbPassword
       PGVECTOR_DATABASE=testdb01

(Optional) Disable the default database and Weaviate containers

To avoid running the default PostgreSQL database and Weaviate containers on ECS, which saves traffic and storage space, edit the .env and docker-compose.yaml files.

Edit the .env file

  1. (Optional) If you have not already done so, copy the example configuration file:

    Important

    Skip this step if you already copied the .env.example file during configuration.

       cd /root/dify/docker
       cp .env.example .env
  2. Edit the .env file and comment out the following line:

       #COMPOSE_PROFILES=${VECTOR_STORE:-weaviate}

Edit the docker-compose.yaml file

  1. Back up the docker-compose.yaml file:

       cd /root/dify/docker
       cp docker-compose.yaml docker-compose.yaml.bak
  2. Edit the docker-compose.yaml file and make the following changes: Comment out - db under depends_on: in the API service: Comment out - db under depends_on: in the worker service: Add profiles: - pg to the PostgreSQL database section: Comment out - '' under profiles: in the Weaviate section:

       # API service
       api:
         ......
         depends_on:
           #- db
           - redis
       # worker service
       # The Celery worker for processing the queue.
       worker:
         ......
         depends_on:
           #- db
           - redis
       # The postgres database.
       db:
         image: postgres:15-alpine
         profiles:
           - pg
       # The Weaviate vector store.
       weaviate:
         image: semitechnologies/weaviate:1.19.0
         profiles:
           #- ''
           - weaviate

Start Dify

Start Dify:

cd /root/dify/docker
docker compose -f docker-compose.yaml up -d

Step 3: Access the Dify service

  1. Open http://<ECS public IP address>/install in your browser.

    Note

    If the page fails to load, refresh it several times. Dify is initializing the storage table structure and related data.

  2. Follow the on-screen prompts to Set Administrator Account by entering an email address, username, and password. This registers you on the Dify platform.

Step 4: Add and configure AI models

This section uses Tongyi Qianwen as an example.

Add Tongyi Qianwen through an API key

  1. Log on to the Dify platform.

  2. In the upper-right corner, click User Name > Settings.

  3. On the Settings page, go to Model Provider > Tongyi Qianwen > (settings).

  4. On the Tongyi Qianwen Settings page, click the link to retrieve the API key from Alibaba Cloud Bailian.

  5. Enter the API key and click Save.

image

(Optional) Deploy Tongyi Qianwen on ECS with Ollama

If you purchased a GPU-based ECS instance, deploy the LLM service locally through Ollama.

  1. Create a container named ollama and expose port 11434: The command returns a container ID, for example:

       docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
       a2201932a0c10da50845a85fc8e00c2178d8b4d0936a92f383b284a7ce51****
  2. Enter the container and pull the models. This example uses qwen2.5-coder:7b (LLM) and rjmalagon/gte-qwen2-7b-instruct:f16 (text embedding):

       docker exec -it a2201932a0c10da50845a85fc8e00c2178d8b4d0936a92f383b284a7ce51**** /bin/bash
    
       ollama pull qwen2.5-coder:7b
    
       ollama pull rjmalagon/gte-qwen2-7b-instruct:f16
  3. Configure the ECS security group. Add port 11434/11434 in the Inbound Direction and set the authorization object to the ECS public IP address.

  4. Log on to the Dify platform.

  5. Go to User Name > Settings > Model Provider > Ollama > Add Model.

  6. Add the following models: Use the default values for other parameters.

    Model typeModel NameBase URL
    LLMqwen2.5-coder:7bhttp://<ECS public IP address>:11434
    Text Embeddingrjmalagon/gte-qwen2-7b-instruct:f16http://<ECS public IP address>:11434

Step 5: Create a knowledge base

A dedicated knowledge base enables the AI Q&A application to answer questions with greater accuracy and relevance.

Upload documents

  1. Go to Knowledge Base > Create Knowledge Base > Import Existing Text > Select File > Next to upload your prepared file.

image
  1. Click Next and follow the on-screen guide to perform Text Segmentation And Traffic Scrubbing. You can use the default settings. Dify automatically scrubs, segments, and indexes the uploaded documents, improving the Q&A application's retrieval accuracy.

Verify the knowledge base in ApsaraDB RDS for PostgreSQL

After setting up the knowledge base, verify its content in the ApsaraDB RDS for PostgreSQL database and confirm the index for each knowledge base table.

  1. Connect to the ApsaraDB RDS for PostgreSQL database used by Dify. For connection instructions, see Connect to an ApsaraDB RDS for PostgreSQL instance.

  2. Query the knowledge base IDs:

       SELECT * FROM datasets;
  3. Construct the table name from the knowledge base ID: replace - with _ in the ID, add the prefix embedding_vector_index_, and append the suffix _nod. For example:

       SELECT * FROM embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod;
  4. Confirm the knowledge base index. By default, Dify creates an HNSW index for each knowledge base table to speed up vector similarity queries through the pgvector plug-in. The system uses the following SQL statement for vector similarity queries: To check whether the index exists and its parameters meet your recall rate requirements, run: For details on how the HNSW index parameters m and ef_construction affect recall rate, see pgvector performance testing (based on HNSW index).

       SELECT
           meta,
           text,
           embedding <=> $1 AS distance
       FROM
           embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod
       ORDER BY
           distance
       LIMIT
           $2;
       SELECT *
       FROM pg_indexes
       WHERE tablename = 'embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod';
  5. (Optional) If the index was not created automatically or the default parameters do not meet your recall rate requirements, create the index manually.

    1. Delete the existing index (if needed):

      DROP INDEX IF EXISTS embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod;
    2. Create a new HNSW index:

      CREATE INDEX ON embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = 16, ef_construction = 100);
    Note

    Replace embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod with your actual table name.

Step 6: Create an AI Q&A application

This section uses the Question Classifier + Knowledge + Chatbot template as an example.

  1. Go to Studio > Create From Application Template.

image
  1. Find the Question Classifier + Knowledge + Chatbot template and click Use This Template.

  2. Set the application name and icon, then click Create.

  3. On the Studio page, select the newly created application card to open the workflow editor.

  4. Configure the workflow: remove the Answer module, keep a Knowledge Retrieval module, and set the AI models for the Question Classifier and LLM modules to Tongyi Qianwen.

image

When configuring Tongyi Qianwen, set the Top P value to less than 1.

image
  1. Customize the Question Classifier module. For example, if your knowledge base contains PostgreSQL information:

    • For PostgreSQL-related questions, combine the knowledge base with the Tongyi Qianwen LLM for analysis and summarization.

    • For non-PostgreSQL questions, use the Tongyi Qianwen LLM directly.

image
  1. Update the Knowledge Retrieval module by adding the PostgreSQL knowledge base you created earlier.

image
  1. Click Preview in the upper-right corner to test the Q&A application. Once it works correctly, click Publish to release it.