🗂️ AgensGraph 图数据库

基于 PostgreSQL 的企业级图数据库

欢迎! 你的 AgensGraph 实例已在 Lazycat 平台成功部署。本页面将帮助你快速上手使用图数据库。

📋 什么是 AgensGraph?

AgensGraph 是一款基于 PostgreSQL 的多模型图数据库,完美结合了关系型数据库和图数据库的优势。它支持标准 SQL 查询和 Cypher 图查询语言,让你可以在同一个数据库中同时使用关系模型和图模型。

🔗 图+关系双模型

在同一查询中混合使用 SQL 和 Cypher

🚀 PostgreSQL 兼容

完全兼容 PostgreSQL 生态系统

⚡ 高性能

优化的图遍历和查询性能

💼 企业级

支持 ACID 事务和数据持久化

🔌 连接信息

参数 说明
主机 agensgraph.{your-box}.heiyu.space 从 Lazycat 管理台获取
端口 5432 PostgreSQL 默认端口
数据库 agens 默认数据库名
用户名 postgres 默认管理员用户
密码 agens 默认密码(建议修改)

🚀 快速开始

1. 连接数据库

使用 agens 客户端或 psql 连接:

# 使用 agens 客户端 agens -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens # 或使用 psql PGPASSWORD=agens psql -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens

2. 创建图空间

-- 创建图 CREATE GRAPH demo_graph; -- 设置默认图路径 SET graph_path = demo_graph; -- 创建节点标签和关系标签 CREATE VLABEL person; CREATE ELABEL knows;
💡 提示: 使用 \dvl 查看节点标签,\del 查看关系标签,SHOW graph_path; 查看当前图路径。

3. 插入图数据

-- 插入节点 CREATE (:person {id: 1, name: 'Alice', city: 'Shanghai'}); CREATE (:person {id: 2, name: 'Bob', city: 'Shanghai'}); CREATE (:person {id: 3, name: 'Carol', city: 'Beijing'}); CREATE (:person {id: 4, name: 'Dave', city: 'Shenzhen'}); -- 插入转介绍关系 MATCH (a:person {name: 'Alice'}), (b:person {name: 'Bob'}) CREATE (a)-[:knows {channel: 'wechat', since: 2019}]->(b); MATCH (a:person {name: 'Bob'}), (b:person {name: 'Carol'}) CREATE (a)-[:knows {channel: 'event', since: 2021}]->(b); MATCH (a:person {name: 'Alice'}), (b:person {name: 'Dave'}) CREATE (a)-[:knows {channel: 'referral', since: 2018}]->(b);

4. 查询图数据

-- 查找两跳内的潜在客户 MATCH (origin:person {name: 'Alice'})-[:knows*1..2]->(target:person) WHERE origin <> target RETURN DISTINCT target.name AS candidate, target.city AS city ORDER BY candidate; -- 查找最短链路 MATCH path = shortestPath( (start:person {name: 'Alice'})-[:knows*1..5]->(target:person {name: 'Carol'}) ) RETURN nodes(path) AS hop_nodes, relationships(path) AS hop_edges;

🎯 实战案例:客户转介绍分析

AgensGraph 非常适合分析社交网络、推荐系统等场景。以下示例展示如何分析客户转介绍关系:

混合 SQL 和图查询

-- 创建补充信息表 CREATE TABLE person_metrics ( person_id int PRIMARY KEY, total_orders int, last_order_amount numeric ); INSERT INTO person_metrics VALUES (1, 15, 1200), (2, 8, 760), (3, 25, 3100), (4, 2, 200); -- SQL + Cypher 混合查询 SELECT graph_result.p->>'name' AS customer, m.total_orders, m.last_order_amount FROM person_metrics m, LATERAL ( MATCH (p:person)-[:knows]->(:person {name: 'Carol'}) RETURN p ) AS graph_result WHERE m.person_id = (graph_result.p->>'id')::int AND m.total_orders >= 5 ORDER BY m.last_order_amount DESC;
🔍 关键点: AgensGraph 允许你在 SQL 查询中嵌入 Cypher 图遍历,充分发挥两种模型的优势。

💾 数据存储

位置 路径
容器内部 /var/lib/postgresql/data
宿主机路径 /data/appvar/cloud.lazycat.app.{user}.agensgraph/data
⚠️ 注意: 数据已持久化到宿主机,重启容器不会丢失数据。定期备份数据库以确保数据安全。

🔧 常用管理命令

图管理

-- 查看所有图 SELECT * FROM ag_graph; -- 切换图 SET graph_path = your_graph; -- 删除图(级联删除所有数据) DROP GRAPH demo_graph CASCADE; -- 删除标签 DROP VLABEL person CASCADE; DROP ELABEL knows CASCADE;

数据库管理

-- 查看连接信息 \conninfo -- 列出所有数据库 \l -- 列出所有表 \dt -- 查看内置图函数 \df cypher.* -- 退出 \q

📚 学习资源

❓ 常见问题

如何修改默认密码?

ALTER USER postgres WITH PASSWORD 'your_new_password';

如何查看当前图路径?

SHOW graph_path;

节点属性如何访问?

使用 ->> 操作符访问节点属性(返回文本),使用 -> 返回 JSON:

-- 在 MATCH 中返回文本 MATCH (p:person {name: 'Alice'}) RETURN p->>'name' AS name_text; -- 在 MATCH 中返回 JSON MATCH (p:person {name: 'Alice'}) RETURN p->'city' AS city_json;

如何备份数据库?

# 备份整个数据库 pg_dump -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens > backup.sql # 恢复数据库 psql -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens < backup.sql
Welcome! Your AgensGraph instance has been successfully deployed on Lazycat platform. This page will help you get started with graph database quickly.

📋 What is AgensGraph?

AgensGraph is a multi-model graph database based on PostgreSQL, perfectly combining the advantages of relational and graph databases. It supports standard SQL queries and Cypher graph query language, allowing you to use both relational and graph models in the same database.

🔗 Dual Model

Mix SQL and Cypher in the same query

🚀 PostgreSQL Compatible

Full compatibility with PostgreSQL ecosystem

⚡ High Performance

Optimized graph traversal and query performance

💼 Enterprise Ready

ACID transactions and data persistence

🔌 Connection Information

Parameter Value Description
Host agensgraph.{your-box}.heiyu.space Get from Lazycat console
Port 5432 PostgreSQL default port
Database agens Default database name
Username postgres Default admin user
Password agens Default password (recommend changing)

🚀 Quick Start

1. Connect to Database

Use agens client or psql to connect:

# Using agens client agens -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens # Or using psql PGPASSWORD=agens psql -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens

2. Create Graph

-- Create graph CREATE GRAPH demo_graph; -- Set default graph path SET graph_path = demo_graph; -- Create vertex and edge labels CREATE VLABEL person; CREATE ELABEL knows;
💡 Tip: Use \dvl to view vertex labels, \del for edge labels, and SHOW graph_path; to check current graph path.

3. Insert Graph Data

-- Insert nodes CREATE (:person {id: 1, name: 'Alice', city: 'Shanghai'}); CREATE (:person {id: 2, name: 'Bob', city: 'Shanghai'}); CREATE (:person {id: 3, name: 'Carol', city: 'Beijing'}); CREATE (:person {id: 4, name: 'Dave', city: 'Shenzhen'}); -- Insert referral relationships MATCH (a:person {name: 'Alice'}), (b:person {name: 'Bob'}) CREATE (a)-[:knows {channel: 'wechat', since: 2019}]->(b); MATCH (a:person {name: 'Bob'}), (b:person {name: 'Carol'}) CREATE (a)-[:knows {channel: 'event', since: 2021}]->(b); MATCH (a:person {name: 'Alice'}), (b:person {name: 'Dave'}) CREATE (a)-[:knows {channel: 'referral', since: 2018}]->(b);

4. Query Graph Data

-- Find potential targets within 2 hops MATCH (origin:person {name: 'Alice'})-[:knows*1..2]->(target:person) WHERE origin <> target RETURN DISTINCT target.name AS candidate, target.city AS city ORDER BY candidate; -- Find shortest referral path MATCH path = shortestPath( (start:person {name: 'Alice'})-[:knows*1..5]->(target:person {name: 'Carol'}) ) RETURN nodes(path) AS hop_nodes, relationships(path) AS hop_edges;

🎯 Use Case: Customer Referral Analysis

AgensGraph is ideal for analyzing social networks, recommendation systems, and more. This example shows how to analyze customer referral relationships:

Mixing SQL and Graph Queries

-- Create supplementary metrics table CREATE TABLE person_metrics ( person_id int PRIMARY KEY, total_orders int, last_order_amount numeric ); INSERT INTO person_metrics VALUES (1, 15, 1200), (2, 8, 760), (3, 25, 3100), (4, 2, 200); -- SQL + Cypher hybrid query SELECT graph_result.p->>'name' AS customer, m.total_orders, m.last_order_amount FROM person_metrics m, LATERAL ( MATCH (p:person)-[:knows]->(:person {name: 'Carol'}) RETURN p ) AS graph_result WHERE m.person_id = (graph_result.p->>'id')::int AND m.total_orders >= 5 ORDER BY m.last_order_amount DESC;
🔍 Key Point: AgensGraph allows you to embed Cypher graph traversals in SQL queries, leveraging the strengths of both models.

💾 Data Storage

Location Path
Container Internal /var/lib/postgresql/data
Host Path /data/appvar/cloud.lazycat.app.{user}.agensgraph/data
⚠️ Note: Data is persisted to the host machine. Container restarts will not lose data. Regular backups are recommended for data safety.

🔧 Common Management Commands

Graph Management

-- List all graphs SELECT * FROM ag_graph; -- Switch graph SET graph_path = your_graph; -- Drop graph (cascade delete all data) DROP GRAPH demo_graph CASCADE; -- Drop labels DROP VLABEL person CASCADE; DROP ELABEL knows CASCADE;

Database Management

-- Show connection info \conninfo -- List all databases \l -- List all tables \dt -- Show built-in graph functions \df cypher.* -- Exit \q

❓ FAQ

How to change default password?

ALTER USER postgres WITH PASSWORD 'your_new_password';

How to check current graph path?

SHOW graph_path;

How to access vertex properties?

Use ->> for text values and -> for JSON values:

-- Return text within MATCH MATCH (p:person {name: 'Alice'}) RETURN p->>'name' AS name_text; -- Return JSON within MATCH MATCH (p:person {name: 'Alice'}) RETURN p->'city' AS city_json;

How to backup database?

# Backup entire database pg_dump -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens > backup.sql # Restore database psql -h agensgraph.{your-box}.heiyu.space -p 5432 -U postgres -d agens < backup.sql