之前在CSDN閱讀資料時,發(fā)現(xiàn)有人問怎么把 postgresql數(shù)據(jù)庫 的表 跟TimescaleDB 時序庫的表 join在一起,正好我在查詢數(shù)據(jù)的時候遇到過這個問題 ,我說一下我的解決方案
我選擇的是postgresql數(shù)據(jù)庫的fdw功能(postgres_fdw插件)
**
一 安裝postgres_fdw插件
1.1安裝postgres_fdw插件
**
su – postgres
-bash-4.2$ psql
postgres=# \c hrmwv2 #(數(shù)據(jù)庫名字)
Create extension "postgres_fdw";
也可以在連接數(shù)據(jù)庫的工具中執(zhí)行

1.2 查看已安裝插件命令
select * from pg_available_extensions;
**
二 創(chuàng)建外部連接(TimescaleDB數(shù)據(jù)庫)
**
需要連接TimescaleDB數(shù)據(jù)庫 信息:(虛構(gòu))
ip :170.0.0.32 端口:5432
數(shù)據(jù)庫名: hrmw 用戶名:postgres
2.1創(chuàng)建于TimescaleDB的外部鏈接
--創(chuàng)建外部服務(wù)器
-- 括號里的三個參數(shù),分別是timescaledb的ip、端口和數(shù)據(jù)庫名稱
CREATE SERVER timescale_db FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '170.0.0.32', port '5432', dbname 'hrmw');
--創(chuàng)建用戶映射
-- 括號里的兩個參數(shù),分別是timescaledb數(shù)據(jù)庫的用戶名、密碼
create user mapping for postgres server timescale_db options(user 'postgres', password '數(shù)據(jù)庫密碼');
2.2 查看外部鏈接命令
select * from pg_foreign_server;
結(jié)果:

#一般fwd出問題就看看這里 是否配置正確
srvname:--你建的鏈接名
srvoptions:--你要鏈接的timescaledb時序庫的信息
2.3 刪除fdw外部鏈接
這里刪除要一步步的刪或者直接使用級聯(lián)刪除的方法
drop server timescale_db CASCADE;
如果不用級聯(lián),直接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.
**
三 創(chuàng)建外部表
**
3.1 創(chuàng)建外部表(你需要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 --你創(chuàng)建的外部鏈接名字
options (table_name '時序庫的表名');
如果你沒有進到pg相應(yīng)的模式下,需指定模式
3.2 刪除外部表命令
跟普通表一樣
DROP FOREIGN TABLE tb_fdw_timescale_target;
**
四 檢查外部表
**
去業(yè)務(wù)打開你建的外部表是否有數(shù)據(jù), 如果有數(shù)據(jù)則表明外部表創(chuàng)建成功,你就可以跟業(yè)務(wù)庫的一起join了

這個錯誤就是你之前配置要連接的TimescaleDB數(shù)據(jù)庫 配置錯誤了,改的話我目前知道的是只能級聯(lián)刪除fdw,重新建了
當然 fdw的功能遠遠不止這些,還可以很Mysql數(shù)據(jù)庫,oracle等數(shù)據(jù)庫
到此這篇關(guān)于postgresql 數(shù)據(jù)庫 與TimescaleDB 時序庫 join 在一起的文章就介紹到這了,更多相關(guān)postgresql與TimescaleDB 時序庫 join內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 淺析postgresql 數(shù)據(jù)庫 TimescaleDB 修改分區(qū)時間范圍
- postgresql數(shù)據(jù)庫 timescaledb 時序庫 把大數(shù)據(jù)量表轉(zhuǎn)換為超表的問題