怎么将postgresql数据库与TimescaleDB时序库 join 在一起

怎么将postgresql数据库与TimescaleDB时序库 join 在一起

这期内容当中小编将会给大家带来有关怎么将postgresql数据库与TimescaleDB时序库 join 在一起,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一 安装postgres_fdw插件

1.1安装postgres_fdw插件
**

su – postgres
-bash-4.2$ psql
postgres=# \c hrmwv2 #(数据库名字)
Create extension "postgres_fdw";

也可以在连接数据库的工具中执行

怎么将postgresql数据库与TimescaleDB时序库 join 在一起

1.2 查看已安装插件命令

select * from pg_available_extensions;

**

二 创建外部连接(TimescaleDB数据库)

**
需要连接TimescaleDB数据库 信息:(虚构)
ip :170.0.0.32 端口:5432
数据库名: hrmw 用户名:postgres

2.1创建于TimescaleDB的外部链接

--创建外部服务器
-- 括号里的三个参数,分别是timescaledb的ip、端口和数据库名称
CREATE SERVER timescale_db FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '170.0.0.32', port '5432', dbname 'hrmw');
--创建用户映射
-- 括号里的两个参数,分别是timescaledb数据库的用户名、密码
create user mapping for postgres server timescale_db options(user 'postgres', password '数据库密码');

2.2 查看外部链接命令

select * from pg_foreign_server;

结果:

怎么将postgresql数据库与TimescaleDB时序库 join 在一起

#一般fwd出问题就看看这里 是否配置正确
srvname:--你建的链接名
srvoptions:--你要链接的timescaledb时序库的信息

2.3 删除fdw外部链接
这里删除要一步步的删或者直接使用级联删除的方法

drop server timescale_db CASCADE;

如果不用级联,直接drop timescale_db ,是删不掉的,报错如下:

> ERROR:  cannot drop server timescale_db because other objects depend on it
DETAIL:  user mapping for postgres on server timescale_db depends on server timescale_db
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

**

三 创建外部表

**
3.1 创建外部表(你需要join TimescaleDB的 那张表:一模一样的,可以是超表)

CREATE FOREIGN TABLE tb_fdw_timescale_target 
 (
 collect_time timestamp(6),
 id varchar(36) ,
 value numeric(12,2) ,
 file_no int4 ,
 create_time timestamp(6)
 )
 server timescale_db --你创建的外部链接名字
 options (table_name '时序库的表名');

如果你没有进到pg相应的模式下,需指定模式
3.2 删除外部表命令
跟普通表一样

DROP FOREIGN TABLE tb_fdw_timescale_target;

**

四 检查外部表

**
去业务打开你建的外部表是否有数据, 如果有数据则表明外部表创建成功,你就可以跟业务库的一起join了

怎么将postgresql数据库与TimescaleDB时序库 join 在一起

这个错误就是你之前配置要连接的TimescaleDB数据库 配置错误了,改的话我目前知道的是只能级联删除fdw,重新建了

当然 fdw的功能远远不止这些,还可以很Mysql数据库,oracle等数据库

上述就是小编为大家分享的怎么将postgresql数据库与TimescaleDB时序库 join 在一起了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。