Modeling and querying data with complex relationships, such as in social networks, fraud detection, or knowledge graphs, is cumbersome and inefficient with traditional SQL. PolarDB for PostgreSQL integrates the open source graph engine Apache AGE. This integration lets you use both standard SQL and the industry-standard openCypher graph query language in the same cluster, enabling you to efficiently store, query, and analyze graph data for complex relationship scenarios.
Applicability
The following versions of PolarDB for PostgreSQL are supported:
-
PostgreSQL 16 (minor engine version 2.0.16.8.3.0 and later)
-
PostgreSQL 15 (minor engine version 2.0.15.12.4.0 and later)
-
PostgreSQL 14 (minor engine version 2.0.14.12.24.0 and later)
You can view the minor engine version in the console or by running the SHOW polardb_version; statement. For more information, see View the minor engine version of a cluster. If your cluster does not meet the minor engine version requirements, you can upgrade the minor engine version.
Visualization tool
You can create a visual graph management tool, based on Apache AGE Viewer, in the console to interact with your graph data through a web interface.
Step 1: Create a graph application
-
You can create it in one of two ways:
-
Go to the PolarDB console. In the navigation pane on the left, click PolarDB AI, and then click Create AI Application.

-
Go to the PolarDB console. In the navigation pane on the left, click Clusters. Find the destination cluster that meets the applicability requirements and navigate to the cluster details page. In the navigation pane on the left, choose , and then click Create AI Application.

-
-
On the application purchase page, select the appropriate configurations as needed:
Configuration item
Description
Billing Method
-
Subscription: Pay upfront for a fixed duration. Longer subscriptions offer larger discounts. Best for stable, long-term workloads.
-
Pay-as-you-go: Billed by actual usage duration with no upfront payment. Best for flexible or short-term workloads.
Engine
Fixed to PolarDB.
Region
Select the geographic location where the application is located.
Note-
The region cannot be changed after purchase.
-
The application must be in the same region as your PolarDB for PostgreSQL cluster. Select the same region as the PolarDB for PostgreSQL cluster.
-
We recommend that you create the application in the same region as the ECS instance you want to connect to. Otherwise, they can only communicate over the public network, which may affect performance.
Architecture
Select AI Application.
Ecosystem
Automatically populated based on the source PolarDB cluster.
Source PolarDB Cluster
Select the PolarDB cluster for which you want to create the application.
Version
Automatically populated based on the source PolarDB cluster.
AI Application
Select Graph Management.
Component Set
Customize the number and specifications of Backend Components as needed.
AI Application Name
You can enter a custom application name.
NoteThe name cannot start with http:// or https:// and must be 2 to 256 characters in length.
Network Type
Fixed to VPC.
VPC
Automatically populated based on the source PolarDB cluster.
Zone and vSwitch
Configure the vSwitch for the VPC. We recommend that you select a vSwitch in the same primary zone as the PolarDB for PostgreSQL cluster for optimal network performance.
If the existing vSwitches do not meet your requirements, you can create a vSwitch.
Security Group
Configure the security group for the application.
Quantity
Select the number of applications to purchase.
Note-
You can purchase only one AI application of the same type for each PolarDB for PostgreSQL cluster.
-
This parameter is available only when Billing Method is set to Subscription.
Duration
Select the subscription duration for the application.
NoteThis parameter is available only when Billing Method is set to Subscription.
Auto-renewal
We recommend enabling auto-renewal to prevent service interruptions.
NoteThis parameter is available only when Billing Method is set to Subscription.
-
-
After the purchase is complete, return to the AI Applications page of the cluster to view the new application.
NoteThe application takes 3 to 5 minutes to create.
Step 2: Connect to the graph application
-
Configure the application whitelist: On the AI Applications list page, click your Application ID to navigate to the application details page. On the Whitelist tab, you can Add Whitelist, Select Security Groups, or Configure an existing whitelist group.
Note-
The application whitelist is independent of the cluster whitelist and must be configured separately.
-
If your ECS instance needs to access the application, you can view the IP address of the ECS instance on the ECS Instance Details page and add it to the IP whitelist.
-
If your ECS instance and the application are in the same VPC, you can add the private IP address of the ECS instance or its VPC CIDR block.
-
If your ECS instance and the application are not in the same VPC, you can add the public IP address of the ECS instance or the security group where the ECS instance is located.
-
-
If your on-premises server, computer, or other cloud server needs to access the application, add its public IP address to the IP whitelist.

-
-
Obtain the endpoint: On the AI Applications list page, click your Application ID to navigate to the application details page. On the Basic Information tab, you can view the Private Endpoint in the Topology section.
Note-
You must request a public endpoint separately. Click the Request button to submit a request.
-
The public endpoint provides only an IP address and a port, not a domain name. If you need a domain name, you can bind one yourself.

-
Step 3: Create the extension and configure the database
-
Create the extension: Run the following statement using a privileged account.
NoteThe
ageextension does not currently support manual creation. To use this feature, submit a ticket to request its creation.CREATE EXTENSION age; -
Configure the database: For each connection, you must add
ag_catalogto thesearch_pathto simplify queries and load the extension using theget_cypher_keywordsfunction:NoteWhen you use a Data Management Service (DMS) client to set the
search_path, compatibility issues may occur. You can use PolarDB-Tools to run the relevant statements.SET search_path = ag_catalog, "$user", public;Use a privileged account to set the database parameters to permanently load the extension. This simplifies the process because you do not need to repeat the preceding operations for each connection.
ALTER DATABASE <dbname> SET search_path = "$user", public, ag_catalog; ALTER DATABASE <dbname> SET session_preload_libraries TO 'age'; -
(Optional) Allow regular users to use AGE: Grant the
USAGEpermission to regular users in theag_catalogschema.GRANT USAGE ON SCHEMA ag_catalog TO <username>;If a regular user has only read and write permissions, you must also grant the
CREATEpermission to create tables.GRANT CREATE ON DATABASE <dbname> TO <username>;
Step 4: Create a graph and insert data
-
Create a graph: You must create a graph before you can use it. To do this, use the
create_graphfunction in theag_catalognamespace.Syntax:
SELECT create_graph('<graph_name>');Example:
SELECT ag_catalog.create_graph('moviedb'); -
Insert data: Use the following SQL statement to insert sample data into the
moviedbgraph:SELECT * FROM cypher('moviedb', $$ CREATE (matrix:Movie {title: 'The Matrix', released: 1997}) CREATE (cloudAtlas:Movie {title: 'Cloud Atlas', released: 2012}) CREATE (forrestGump:Movie {title: 'Forrest Gump', released: 1994}) CREATE (keanu:Person {name: 'Keanu Reeves', born: 1964}) CREATE (robert:Person {name: 'Robert Zemeckis', born: 1951}) CREATE (tom:Person {name: 'Tom Hanks', born: 1956}) CREATE (tom)-[:ACTED_IN {roles: ['Forrest']}]->(forrestGump) CREATE (tom)-[:ACTED_IN {roles: ['Zachry']}]->(cloudAtlas) CREATE (robert)-[:DIRECTED]->(forrestGump) $$) AS (result1 agtype);This includes six nodes, three with the label Movie and three with the label Person. It also includes three edges, two with the label ACTED_IN and one with the label DIRECTED. The relationship graph is shown below:

Step 5: Develop the application
-
Develop the application: You can access the application directly over the Internet. On the AI Applications list page, click Develop Application to open the public endpoint of the graph application's visualization tool. Alternatively, you can enter the public IP address and port of the application in your browser's address bar to access the visualization tool.
NoteFirst, add your public IP address to the application whitelist.

-
On the visualization tool's logon page, enter the following information:
-
host: Select the endpoint of your PolarDB cluster.
-
Database Name: Enter a database within the cluster. If you do not have a suitable database, return to the cluster details page to create a database.
-
User name: Enter a database account within the cluster. Make sure it has the necessary access permissions for the database.
-
password: Enter the password for the database account.

-
-
Query data: In Cypher, use the
MATCHandRETURNkeywords to query data.-
MATCHperforms pattern matching to find content that matches the specified pattern. -
The
RETURNkeyword specifies the values or results you want to return from the Cypher query.
Syntax:
SELECT * FROM cypher('graph_name', $$ MATCH <patterns>RETURN <variables> $$) AS (result1 agtype);Example: Enter the following Cypher query statement at the top of the visualization tool:
SELECT * FROM cypher('moviedb', $$ MATCH (m:Person) RETURN m $$) AS (result1 agtype);
-
-
Verification: After the statement is successfully executed, the three Person nodes of the
moviedbgraph appear in the visualization area below.
Step 6: (Optional) Release the graph application
When the visualization tool is no longer needed, you can release it to save costs. In the AI Applications list, find the destination graph application, and in the Actions column, click Release Application.
Releasing a graph application only deletes the visualization tool that provides the web interface. It does not delete any graph data stored in your PolarDB cluster. After the application is released, its configurations, such as the public endpoint, are lost and cannot be recovered.
How it works
-
Core engine: The PolarDB graph database feature is implemented based on the PostgreSQL
ageextension, which is provided by the Apache AGE project and is compatible with the OpenCypher query syntax. -
Hybrid query: The
ageextension lets you manage both relational data (standard tables) and graph data in the same database. These two types of data can coexist and be queried separately. -
Query execution: Cypher queries are not executed directly. Instead, they are passed as string parameters to a PostgreSQL function named
cypher(). PolarDB parses this string, executes the Cypher command in the specified graph, and returns the results as a standard SQL row set. -
Data type: The result columns returned by a query are usually of the
agtypetype. This is a custom data type similar to JSONB that is used to encapsulate structured information of graph elements, such as nodes, edges, and paths. In an application, you can typically handle it as a JSON string.
Billing
-
Component fees: Graph applications are charged for backend components. The fees are calculated based on the component specifications (CPU and memory) and the subscription duration you select.
-
Storage fees: Data and files generated by graph applications are stored in the storage space of the PolarDB for PostgreSQL cluster.
-
Traffic and bandwidth: No fees are charged.
References
-
Graph Database Quick Start: Interact directly with the graph database using Cypher statements through an SQL client, such as psql. This method is suitable for backend development and automated scripting scenarios.
-
SQL reference: The SQL syntax is based on Apache AGE with modifications for Graph Database.