tencent cloud

TencentDB for PostgreSQL

DocumentationTencentDB for PostgreSQLAI CapabilitiesPractical TutorialBuilding AI Applications with PostgreSQL + Dify

Building AI Applications with PostgreSQL + Dify

Download
Focus Mode
Font Size
Last updated: 2026-06-08 10:32:14

Overview

Dify is an open-source LLM application development platform that supports building AI applications such as RAG, Agent, and Workflow through a visual interface. By integrating TencentDB for PostgreSQL as the vector storage and knowledge base backend, you can rapidly build enterprise-grade AI applications.

Architecture

┌──────────────────────────────────────────────────┐
│ Dify Platform │
│ ┌──────────┐ ┌───────────┐ ┌──────────────┐ │
│ │ RAG Engine │ │ Agent │ │ Workflow Engine │ │
│ └─────┬────┘ └─────┬─────┘ └──────┬───────┘ │
│ └─────────────┼───────────────┘ │
└──────────────────────┼─────────────────────────────┘
┌─────────────┼─────────────┐
▼ ▼ ▼
┌──────────────┐ ┌──────────┐ ┌──────────────┐
│ PostgreSQL │ │ LLM │ │ External Tools │
+ pgvector │ │ (Reasoning) │ │ (API)
(Vector Storage) │ │ │ │ │
└──────────────┘ └──────────┘ └──────────────┘

Deploying Dify

Docker Compose Deployment

# Clone Dify
git clone https://github.com/langgenius/dify.git
cd dify/docker

# Configure Environment Variables
cp .env.example .env

Configuring PostgreSQL as a Vector Database

Configure in .env:
# Select pgvector as the Vector Database Type
VECTOR_STORE=pgvector

# PostgreSQL Connection Information
PGVECTOR_HOST=your-pg-instance.tencentcdb.com
PGVECTOR_PORT=5432
PGVECTOR_USER=dify_user
PGVECTOR_PASSWORD=your-password
PGVECTOR_DATABASE=dify_vectors

Database Initialization

-- Create a Dify-specific database
CREATE DATABASE dify_vectors;

-- Execute after connecting to the dify_vectors database:
CREATE EXTENSION IF NOT EXISTS vector;

-- Create a Dify user
CREATE USER dify_user WITH PASSWORD 'secure-password';
GRANT ALL PRIVILEGES ON DATABASE dify_vectors TO dify_user;
GRANT ALL ON SCHEMA public TO dify_user;

Creating a Knowledge Base

Configuring in Dify

1. Log in to the Dify console.
2. Go to Knowledge BaseCreate Knowledge Base.
3. Upload documents (PDF, TXT, Markdown / HTML are supported).
4. Select an embedding model (BGE-M3 is recommended).
5. Vector data is automatically stored in PostgreSQL.

Underlying Storage Schema

Tables automatically created by Dify in PostgreSQL (the specific table schema is subject to the current Dify version):
-- Vector tables automatically created by Dify (illustrative)
CREATE TABLE embedding_documents (
id UUID PRIMARY KEY,
dataset_id UUID,
document_id UUID,
content TEXT,
embedding vector(1024),
metadata JSONB,
created_at TIMESTAMPTZ
);

-- HNSW index
CREATE INDEX ON embedding_documents
USING hnsw (embedding vector_cosine_ops);

Building RAG Applications

Creating a RAG Chat Application

1. Create an application → Select Chat Assistant.
2. Add knowledge base context.
3. Configure the search policy: .
Vector Search (Semantic Search).
Full-text search (keyword matching).
Hybrid Search (recommended).
4. Set the system prompt.
5. Select a large language model (LLM), such as Tencent Hunyuan, Tongyi, Qwen, or other models that have been enabled.

Advanced Search Configuration

Parameter
Description
Recommended Value
Top K
Number of most relevant documents returned
3 – 5
Score threshold
Minimum similarity score
0.5 – 0.7
Reranking
Whether to use the Rerank model
Recommended to enable
Chunk size
Chunk size
500 – 1000 characters

Building Agent Applications

Configuring Agent Tools

You can add a database query tool to the Agent:
# Custom Tool Configuration
name: pg_query
description: Query the PostgreSQL database.
parameters:
- name: sql
type: string
description: The SQL query to be executed.
Note:
Directly passing SQL to an Agent for execution carries injection risks. It is recommended to switch to an allowlist function/view invocation model: expose only RPC functions with fixed signatures (such as rag_search and ai_answer), and have the backend perform parameter validation before accessing the database.

Integrating with tencentdb_ai

Invoke the PostgreSQL REST API via Dify's HTTP tool:
POST https://your-pg-api/rpc/ai_answer
{"question": "{{user_input}}"}

Workflow Applications

Suitable for complex, multi-step AI processing workflows:
User Input → Intent Recognition → Conditional Branching
├── Knowledge Q&A → RAG Search → LLM Generation
├── Data Query → SQL Generation → Execution → Formatting
└── Operation Command → Tool Invocation → Result Return

Performance Optimization

1. Index Type: Use pgvectorscale DiskANN for large-scale data (>1 million vectors).
2. Connection Pool: Configure PgBouncer to manage connections.
3. Batch Indexing: Import large volumes of documents in batches to avoid memory overflow.
4. Regular Maintenance: Execute VACUUM ANALYZE to keep statistics up to date.

Notes

Dify Community Edition is free, while the Enterprise Edition requires a paid license.
To use pgvector, the PostgreSQL version must be 14 or later.
Ensure that the Dify server and PostgreSQL are in the same VPC.
For production environments, it is recommended that the PostgreSQL specification be 4 cores and 8 GB or higher.
Inject the database password via environment variables or a secrets management service. Hardcoding is prohibited.

References

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback