docker安装postgres扩展age以及使用nodejs连接
官网: https://age.apache.org/age-manual/master/advanced/plpgsql.html
GitHub: https://github.com/apache/age/blob/master/README.md
docker pull apache/age
docker run
–name myPostgresDb
-p 5455:5432
-e POSTGRES_USER=postgresUser
-e POSTGRES_PASSWORD=postgresPW
-e POSTGRES_DB=postgresDB
-d
apache/age
LOAD ‘age’;
SET search_path = ag_catalog, “$user”, public; # 简化查询,设置用户public,后面对该用户进行授权
grant usage on schema ag_catelog to db_user(public) # 授权用户 public
新开一个窗口
docker exec -it age psql -d postgresDB -U PostgresUser
ln -s /usr/lib/postgresql/16/lib/age.so /usr/lib/postgresql/16/lib/plugins/age.so # 创建符号链接,允许非超级用户加载age库
这里替换为你自己的版本,以及mkdir一下plugins目录
使用age的nodejs-sdk连接,没有npm包。整个下载下来copy到项目中,可以link,也可以直接引入。
pnpm install pg config debug
config/default.json
{"age":{"user": "postgresUser","host": "127.0.0.1","database": "postgresDB","password": "postgresPW","port": "5455"}
}
import pg from 'pg'
import config from "config"
import debug from 'debug'
import { setAGETypes } from '../nodejs-pg-age/dist/index.js'
const { types, Client } = pg
const dbConfig = config['age']function createDebug(moduleName) {return debug('age').extend(moduleName);
}async function main () {const debug = createDebug('main')const client = new Client(dbConfig)await client.connect()await setAGETypes(client, types)let ret = await client.query(`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);`)if (ret?.rowCount > 0) {debug(ret.rows);}
}main().then((data) => console.log(data)).catch(err => console.error(err))
{"name": "age-crud","version": "1.0.0","description": "","main": "./src/index.js","author": "hjj","license": "ISC","type": "module","scripts": {"start": "DEBUG=age:* DEBUG_DEPTH=5 node ./src/index.js"},"dependencies": {"config": "^3.3.12","debug": "^4.3.7","pg": "^8.12.0"}
}
pgadmin4连接