PolarDB for PostgreSQL integrates the open source graph engine Apache AGE. This integration lets you use standard SQL and the industry-standard openCypher graph query language in the same cluster. You can efficiently store, query, and analyze graph data for complex relationship scenarios.
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: An upfront billing method. When you create an application, you must select resources with defined specifications and pay for the application in advance. The longer the subscription duration, the larger the discount. This billing method is suitable for scenarios with stable long-term business requirements.
Pay-as-you-go: A pay-as-you-go billing method. When you create an application, you must select resources with defined specifications, but you do not need to pay for the application in advance. You are billed based on the actual usage duration. This billing method is suitable for scenarios with flexible business requirements.
Engine
Fixed as PolarDB.
Region
Select the geographic location where the application is located.
NoteYou cannot change the region after the application is purchased.
The application must be in the same region as the PolarDB for PostgreSQL cluster. Therefore, select the same region as the PolarDB for PostgreSQL cluster.
We recommend that you create the application and the ECS instance that you want to connect to in the same region. Otherwise, they cannot communicate with each other over the internal network (private network) and can only communicate over the external network (public network), which prevents optimal performance.
Architecture
Select AI Application.
Ecosystem
The database ecosystem of the source PolarDB cluster is automatically filled in. You do not need to fill it in manually.
Source PolarDB Cluster
Select the PolarDB cluster for which you want to create the application.
Version
The database version of the source PolarDB cluster is automatically filled in. You do not need to fill it in manually.
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 as VPC.
VPC
The VPC of the source PolarDB cluster is automatically filled in. You do not need to fill it in manually.
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.
NoteYou 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
Configure whether to enable auto-renewal. To prevent business interruptions caused by forgetting to renew, we recommend that you enable auto-renewal.
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 system takes 3 to 5 minutes to create the application. Please wait.
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.
NoteThe 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.
NoteYou 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.