┌──────────────────────────────────────────────────┐│ Dify Platform ││ ┌──────────┐ ┌───────────┐ ┌──────────────┐ ││ │ RAG Engine │ │ Agent │ │ Workflow Engine │ ││ └─────┬────┘ └─────┬─────┘ └──────┬───────┘ ││ └─────────────┼───────────────┘ │└──────────────────────┼─────────────────────────────┘│┌─────────────┼─────────────┐▼ ▼ ▼┌──────────────┐ ┌──────────┐ ┌──────────────┐│ PostgreSQL │ │ LLM │ │ External Tools ││ + pgvector │ │ (Reasoning) │ │ (API) ││ (Vector Storage) │ │ │ │ │└──────────────┘ └──────────┘ └──────────────┘
# Clone Difygit clone https://github.com/langgenius/dify.gitcd dify/docker# Configure Environment Variablescp .env.example .env
.env:# Select pgvector as the Vector Database TypeVECTOR_STORE=pgvector# PostgreSQL Connection InformationPGVECTOR_HOST=your-pg-instance.tencentcdb.comPGVECTOR_PORT=5432PGVECTOR_USER=dify_userPGVECTOR_PASSWORD=your-passwordPGVECTOR_DATABASE=dify_vectors
-- Create a Dify-specific databaseCREATE DATABASE dify_vectors;-- Execute after connecting to the dify_vectors database:CREATE EXTENSION IF NOT EXISTS vector;-- Create a Dify userCREATE 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;
-- 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 indexCREATE INDEX ON embedding_documentsUSING hnsw (embedding vector_cosine_ops);
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 |
# Custom Tool Configurationname: pg_querydescription: Query the PostgreSQL database.parameters:- name: sqltype: stringdescription: The SQL query to be executed.
rag_search and ai_answer), and have the backend perform parameter validation before accessing the database.POST https://your-pg-api/rpc/ai_answer{"question": "{{user_input}}"}
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
VACUUM ANALYZE to keep statistics up to date.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback