pg_cron 是一个简单但是使用还算灵活的pg定时任务扩展,以下是一个remote insert 数据的简单试用
环境准备
- docker-compose
注意demo,包含了好几个组件,cstore 以及graphql-engine 可以不用管,但是三者集成起来将会很强大的
version: '3'
services:
graphql-engine:
image: hasura/graphql-engine:v1.1.0-beta.3
ports:
- "8080:8080"
environment:
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:dalong@pgspider-cstore:5432/postgres
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
pgspider-pg_cron:
image: dalongrong/pgspider:pg_cron
volumes:
- "./.pgpass:/var/lib/postgresql/.pgpass"
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
pgspider-cstore:
image: dalongrong/pgspider:cstore
ports:
- "5433:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
- 说明(比较重要)
连接remote server 需要使用pgpass文件,注意此文件的位置,以及权限
位置:~/.pgpass,对于linux 的pg 应该为pg 启动用户的,比如上边的为postgresql ,目录为/var/lib/postgresql/
权限:需要0600
启用pg_cron 扩展
CREATE EXTENSION pg_cron;
使用pg_cron
- remote server 创建表
CREATE TABLE apps (
id SERIAL PRIMARY KEY,
insert_date timestamp without time zone
);
- 添加调度
INSERT INTO cron.job (schedule, command, nodename, nodeport, database, username)
VALUES ('* * * * *', 'insert into apps(insert_date) values(now())', 'pgspider-cstore', 5432, 'postgres', 'postgres');
效果
remote server pgspider-cstore
说明
pg_cron 作为一个pg 的扩展,集成起来还是很不错的,可以做为一个简单的任务调度工具,方便数据分析
参考资料
https://github.com/citusdata/pg_cron
https://www.postgresql.org/docs/current/libpq-pgpass.html
https://github.com/rongfengliang/cstore_fdw-graphql-engine-learning
https://github.com/rongfengliang/pgspider-docker