postgresql database and TimescaleDB time series library join together

postgresql database and TimescaleDB time series library join together

**

postgres_fdw usage

**
When I read the information in CSDN, I found that someone asked how to join the table of the postgresql database with the table of the TimescaleDB time series library. It happened that I encountered this problem when querying the data. Let me talk about my solution.
I choose Is the fdw function of the postgresql database ( postgres_fdw plugin )

**

One install postgres_fdw plugin

1.1 Install postgres_fdw plugin
**

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

You can also execute
Insert picture description here
1.2 View installed plug-in commands in the tool that connects to the database

select * from pg_available_extensions;

**

Two create an external connection (TimescaleDB database)

**
Need to connect to TimescaleDB database information: (fictitious)
ip: 170.0.0.32 port: 5432
database name: hrmw user name: postgres

2.1 External links created in 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 View external link commands

select * from pg_foreign_server;

result:
Insert picture description here

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

2.3 Deleting fdw external links
. Deleting here requires step-by-step deletion or directly use the cascading deletion method

drop  server timescale_db CASCADE;

If you don't need to cascade, you can drop timescale_db directly, it cannot be deleted, and the error is as follows:

> 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.

**

Three create an external table

**
3.1 Create an external table (the table you need to join TimescaleDB: exactly the same, it can be a super table)

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 '时序库的表名');

If you do not enter the corresponding mode of pg, you need to specify the mode.
3.2 Delete external table command
Same as normal table

DROP FOREIGN TABLE tb_fdw_timescale_target;

**

Four check the external table

**
Go to the business to open whether there is data in the external table you built. If there is data, it means that the external table is created successfully, and you can join with the business database

Insert picture description here
This error is that the TimescaleDB database you configured to connect to is configured incorrectly. If you change it, what I currently know is that you can only cascade to delete fdw and rebuild it.

Of course, the function of fdw is far more than these, it can also be a database such as Mysql database, oracle

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111751229