Skip to main content

Apache AGE

Apache AGE is an extension for PostgreSQL that enbales users to leverage a graph database on top of the existing relational databases. As an extension of PostgreSQL, AGE supports all the functonalities and features of PostgreSQL while also offering a graph model to boot.

How to run?​

Running on CGC is very easy. You just need to create a new database and load the AGE extension.

Create volume for the data:

cgc volume create -s 5 -sc <storage_class> pgage-volume

Run database with volume attached:

cgc db create -n pgage-001-prod -v pgage-volume pg-age

After creating the database, you will receive an app token to your database.

warning

Always create the database with a mounted volume. Without -v the database writes to the pod's ephemeral disk, so every restart (cgc resource restart, node maintenance, a crash) initialises PostgreSQL from scratch and all data is lost after a single restart. With a volume (mounted at /var/lib/postgresql/data, which holds PGDATA) the same rows survived cgc resource restart, and they also survived deleting the database and creating it again with the same name and the same volume.

Default configuration​

The default configuration for AGE is set to use the PostgreSQL database engine. The database will be created with the following parameters:

  • POSTGRES_PASSWORD: The password for the PostgreSQL user. This is set to CGC specific app_token that you receive after creating the database.
  • POSTGRES_USER=admin: The user for the PostgreSQL database.
  • POSTGRES_DB=db: The name of the PostgreSQL database. It is always db, it is not set to the app name you provided.
  • POSTGRES_HOST_AUTH_METHOD=trust: Configures PostgreSQL to skip password verification for connections, trusting that authentication has already occurred at the system level. This is convenient for development but creates a significant security risk in production environments.
  • PGDATA=/var/lib/postgresql/data/pgdata: The data directory, a subdirectory of the mounted volume.
info

If you wish to overwrite POSTGRES_PASSWORD and keep it displayed correctly during cgc db list -d, we advice using CGC flag -ce app_token=YOUR_PASSWORD

How to connect to the database​

For more information about CGC SDK and how to connect to the database, please refer to our docs

Installation​

For every connection of AGE you start, you will need to load the AGE extension.

CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

Done! You can now use AGE in your database.

Example usage​

To create a graph, use the create_graph function located in the ag_catalog namespace.

SELECT create_graph('graph_name');

To create a single vertex with label and properties, use the CREATE clause.

SELECT *
FROM cypher('graph_name', $$
CREATE (:label {property:"Node A"})
$$) as (v agtype);
SELECT *
FROM cypher('graph_name', $$
CREATE (:label {property:"Node B"})
$$) as (v agtype);

To create an edge between two nodes and set its properties:

SELECT *
FROM cypher('graph_name', $$
MATCH (a:label), (b:label)
WHERE a.property = 'Node A' AND b.property = 'Node B'
CREATE (a)-[e:RELTYPE {property:a.property + '<->' + b.property}]->(b)
RETURN e
$$) as (e agtype);

And to query the connected nodes:

SELECT * from cypher('graph_name', $$
MATCH (V)-[R]-(V2)
RETURN V,R,V2
$$) as (V agtype, R agtype, V2 agtype);

For more information about AGE, please refer to the official documentation and Github repository