PostgesSQL外部數據封裝FDW

PostgesSQL外部數據封裝FDW

  • 1. FDW外部數據配置(單表)
    • 1.1 遠端數據庫創建測試表
    • 1.2 安裝擴展postges\_fdw
    • 1.3 創建外部服務SERVER
    • 1.4 創建用戶映射USER MAPPING
    • 1.5 創建遠程表FOREIGN TABLE
    • 1.6 數據庫更新測試
  • 2. FDW外部數據配置(用戶)
    • 2.1 遠端數據庫創建測試表
    • 2.2 創建遠程schema
    • 2.3 導入遠程schema
    • 2.4 查看遠程schema表
  • 3. FDW運維操作
    • 3.1 外部表設置只讀操作
    • 3.2 刪除增加外部表列
    • 3.3 模式導入部分表

POSTGRESQL FDW 的名詞是 foreign-data wrapper 外部數據包裝, 此模塊提供的功能與較舊的 dblink 模塊的功能有很大重疊。但是 postgres_fdw 提供了更透明且符合標準的語法來訪問遠程表,并且在許多情況下可以提供更好的性能。

要準備使用 postgres_fdw 進行遠程訪問

  1. 使用 CREATE EXTENSION 安裝 postgres_fdw 擴展。

  2. 使用 CREATE SERVER 創建外部服務器對象,以表示您要連接的每個遠程數據庫。將連接信息指定為服務器對象的選項,但 userpassword 除外。

  3. 使用 CREATE USER MAPPING 為您要允許訪問每個外部服務器的每個數據庫用戶創建用戶映射。將要使用的遠程用戶名和密碼指定為用戶映射的 userpassword 選項。

  4. 使用 CREATE FOREIGN TABLE 或 IMPORT FOREIGN SCHEMA 為您要訪問的每個遠程表創建外部表。外部表的列必須與引用的遠程表匹配。但是,如果將正確的遠程名稱指定為外部表對象的選項,則可以使用與遠程表不同的表和/或列名。

1. FDW外部數據配置(單表)

1.1 遠端數據庫創建測試表

postgres=# create database testdb13;
CREATE DATABASE
postgres=# \lList of databasesName    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestestdb13  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)
postgres=# create user u13 with password 'u13';
CREATE ROLE
postgres=# \c testdb13 u13
Password for user u13: 
You are now connected to database "testdb13" as user "u13".
testdb13=> 
testdb13=> create table t1 (id int,name varchar);
CREATE TABLE
testdb13=> insert into t1 values(1,'A');
INSERT 0 1
testdb13=> select * from t1;id | name 
----+------1 | A
(1 row)
testdb13=> \dt+List of relationsSchema | Name | Type  | Owner | Persistence | Size  | Description 
--------+------+-------+-------+-------------+-------+-------------public | t1   | table | u13   | permanent   | 16 kB | 

1.2 安裝擴展postges_fdw

已安裝擴展

postgres=# \dxList of installed extensionsName   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

安裝報錯,需先在本地軟件安裝

postgres=# create extension postges_fdw;
ERROR:  extension "postges_fdw" is not available
DETAIL:  Could not open extension control file "/pgsql/app/pg16/share/extension/postges_fdw.control": No such file or directory.
HINT:  The extension must first be installed on the system where PostgreSQL is running.

進入到編譯安裝目錄,編輯安裝postgres_fdw

[root@pgdb ~]# cd /pgsql/soft/postgresql-16.1/contrib/postgres_fdw
[root@pgdb postgres_fdw]# ls
connection.c  deparse.c  expected  meson.build  option.o                    postgres_fdw--1.0.sql  postgres_fdw.control  postgres_fdw.o   shippable.c  sql
connection.o  deparse.o  Makefile  option.c     postgres_fdw--1.0--1.1.sql  postgres_fdw.c         postgres_fdw.h        postgres_fdw.so  shippable.o
[root@pgdb postgres_fdw]# make
[root@pgdb postgres_fdw]# make install

安裝擴展postgres_fdw

postgres=# create extension postgres_fdw ;
CREATE EXTENSION
postgres=# \dxList of installed extensionsName     | Version |   Schema   |                    Description                     
--------------+---------+------------+----------------------------------------------------plpgsql      | 1.0     | pg_catalog | PL/pgSQL procedural languagepostgres_fdw | 1.1     | public     | foreign-data wrapper for remote PostgreSQL servers
(2 rows)

1.3 創建外部服務SERVER

創建一個服務器名pg_fdw_server 使用的類型為postgres_fdw 連接的數據庫為 dbname 為 testdb13

postgres=# CREATE SERVER pg_fdw_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '192.168.1.100', port '5432', dbname 'testdb13');
CREATE SERVER

查看創建server

postgres=# \desList of foreign serversName      |  Owner   | Foreign-data wrapper 
---------------+----------+----------------------pg_fdw_server | postgres | postgres_fdw
(1 row)
postgres=# \des+List of foreign serversName      |  Owner   | Foreign-data wrapper | Access privileges | Type | Version |                      FDW options                       | Description 
---------------+----------+----------------------+-------------------+------+---------+--------------------------------------------------------+-------------pg_fdw_server | postgres | postgres_fdw         |                   |      |         | (host '192.168.1.100', port '5432', dbname 'testdb13') | 
(1 row)

1.4 創建用戶映射USER MAPPING


postgres=# CREATE USER MAPPING FOR postgres SERVER pg_fdw_server OPTIONS (user 'u13', password 'u13');
CREATE USER MAPPING
postgres=# \deu+List of user mappingsServer     | User name |          FDW options           
---------------+-----------+--------------------------------pg_fdw_server | u109      | ("user" 'u13', password 'u13')
(1 row)

1.5 創建遠程表FOREIGN TABLE

postgres=# CREATE FOREIGN TABLE fdw_t1(id int,name varchar) SERVER pg_fdw_server OPTIONS (schema_name 'public',table_name 't1');
CREATE FOREIGN TABLE
postgres=# \det+List of foreign tablesSchema | Table  |    Server     |               FDW options               | Description 
--------+--------+---------------+-----------------------------------------+-------------public | fdw_t1 | pg_fdw_server | (schema_name 'public', table_name 't1') | postgres=# select * from fdw_t1;id | name 
----+------1 | A
(1 row)

1.6 數據庫更新測試

遠端數據庫insert數據

postgres=# \c testdb13 u13
Password for user u13: 
You are now connected to database "testdb13" as user "u13".
testdb13=> insert into t1 values(2,'B');
INSERT 0 1

本地數據庫訪問遠程表,實時同步過來。

postgres=# select * from fdw_t1;id | name 
----+------1 | A2 | B
(2 rows)

2. FDW外部數據配置(用戶)

2.1 遠端數據庫創建測試表

testdb13=> create table t2 (id int,name varchar);
CREATE TABLE
testdb13=>  create table t3 (id int,name varchar);
CREATE TABLE
testdb13=> \dt+List of relationsSchema | Name | Type  | Owner | Persistence |    Size    | Description 
--------+------+-------+-------+-------------+------------+-------------public | t1   | table | u13   | permanent   | 16 kB      | public | t2   | table | u13   | permanent   | 8192 bytes | public | t3   | table | u13   | permanent   | 8192 bytes | 
(3 rows)

2.2 創建遠程schema

postgres=# create schema remote_fdw_schema;
CREATE SCHEMA

2.3 導入遠程schema

postgres=# import foreign schema public from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA

2.4 查看遠程schema表

postgres=# \det+ remote_fdw_schema.*List of foreign tablesSchema       | Table |    Server     |               FDW options               | Description 
-------------------+-------+---------------+-----------------------------------------+-------------remote_fdw_schema | t1    | pg_fdw_server | (schema_name 'public', table_name 't1') | remote_fdw_schema | t2    | pg_fdw_server | (schema_name 'public', table_name 't2') | remote_fdw_schema | t3    | pg_fdw_server | (schema_name 'public', table_name 't3') | 
(3 rows)
postgres=# select * from remote_fdw_schema.t2;id | name 
----+------
(0 rows)postgres=# select * from remote_fdw_schema.t1;id | name 
----+------1 | A2 | B
(2 rows)postgres=# select * from remote_fdw_schema.t3;id | name 
----+------
(0 rows)

遠端數據庫更新數據測試

postgres=# \c testdb13 u13
testdb13=> insert into t2 values(22,'BB');
INSERT 0 1
testdb13=> insert into t3 values(33,'CC');
INSERT 0 1

本地數據庫查看遠程schema表

postgres=# select * from remote_fdw_schema.t2;id | name 
----+------22 | BB
(1 row)
postgres=# select * from remote_fdw_schema.t3;id | name 
----+------33 | CC
(1 row)

3. FDW運維操作

3.1 外部表設置只讀操作

postgres=# insert into remote_fdw_schema.t1 values (3,'C');
INSERT 0 1
postgres=# select * from remote_fdw_schema.t1;id | name 
----+------1 | A2 | B3 | C
(3 rows)

修改為只讀操作,false改為true讀寫模式

postgres=# alter foreign table remote_fdw_schema.t1 options (add updatable 'false');
ALTER FOREIGN TABLE
postgres=# insert into remote_fdw_schema.t1 values (4,'D');
ERROR:  foreign table "t1" does not allow inserts

3.2 刪除增加外部表列

刪除列

postgres=# select * from remote_fdw_schema.t1;id | name 
----+------1 | A2 | B3 | C
(3 rows)
postgres=# alter foreign table remote_fdw_schema.t1 drop column name;
ALTER FOREIGN TABLE
postgres=# select * from remote_fdw_schema.t1;id 
----123
(3 rows)

增加列

postgres=# alter foreign table remote_fdw_schema.t1 add column name varchar;
ALTER FOREIGN TABLE
postgres=# select * from remote_fdw_schema.t1;id | name 
----+------1 | A2 | B3 | C
(3 rows)

3.3 模式導入部分表

  • Limit to 指定哪些表導入
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server 
--------+-------+--------
(0 rows)
postgres=# import foreign schema public limit to ( t1,t2) from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema       | Table |    Server     
-------------------+-------+---------------remote_fdw_schema | t1    | pg_fdw_serverremote_fdw_schema | t2    | pg_fdw_server
(2 rows)
  • Except 指定哪些表不要導入
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema | Table | Server 
--------+-------+--------
(0 rows)
postgres=# import foreign schema public except ( t1,t3) from server pg_fdw_server into remote_fdw_schema options ( import_default 'true');
IMPORT FOREIGN SCHEMA
postgres=# \det_ remote_fdw_schema.*List of foreign tablesSchema       | Table |    Server     
-------------------+-------+---------------remote_fdw_schema | t2    | pg_fdw_server
(1 row)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/903086.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/903086.shtml
英文地址,請注明出處:http://en.pswp.cn/news/903086.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

策略模式(Strategy Pattern)詳解

文章目錄 1. 什么是策略模式?2. 為什么需要策略模式?3. 策略模式的核心概念3.1 策略(Strategy)3.2 具體策略(Concrete Strategy)3.3 上下文(Context) 4. 策略模式的結構5. 策略模式的…

在 Vue3 中封裝的 Axios 實例中,若需要為部分接口提供手動取消請求的功能

核心思路 封裝接口時返回 Promise 和 abort 方法: 為需要支持取消的接口返回一個對象,包含 promise 和 abort 方法,用戶可通過 abort 主動中斷請求。使用 AbortController 或 CancelToken: 推薦 AbortController(瀏覽…

Flink介紹——實時計算核心論文之Dataflow論文詳解

引入 在過去的幾篇文章里,我們看到了大數據的流式處理系統是如何一步一步進化的。從最早出現的S4,到能夠做到“至少一次”處理的Storm,最后是能夠做到“正好一次”數據處理的MillWheel。我們會發現,這些流式處理框架,…

Python自動化解決滑塊驗證碼的最佳實踐

1. 引言:滑塊驗證碼的挑戰與自動化需求 滑塊驗證碼(Slider CAPTCHA)是當前互聯網廣泛使用的反爬機制之一,它要求用戶手動拖動滑塊到指定位置以完成驗證。這種驗證方式可以有效阻止簡單的自動化腳本,但對爬蟲開發者來說…

路由與OSPF學習

【路由是跨網段通訊的必要條件】 路由指的是在網絡中,數據包從源主機傳輸到目的主機的路徑選擇過程。 路由通常涉及以下幾個關鍵元素: 1.路由器:是一種網絡設備,負責將數據包從一個網絡傳輸到另一個網絡。路由器根據路由表來決定…

(done) 吳恩達版提示詞工程 5. 推理 (情緒分類,控制輸出格式,輸出 JSON,集成多個任務,文本主題推斷和索引,主題內容提醒)

url: https://www.bilibili.com/video/BV1Z14y1Z7LJ?spm_id_from333.788.videopod.episodes&vd_source7a1a0bc74158c6993c7355c5490fc600&p2 別人的筆記 url: https://zhuanlan.zhihu.com/p/626966526 5. 推理任務(Inferring) 這個視頻是關于…

MySQL VS SQL Server:優缺點全解析

數據庫選型、企業協作、技術生態、云數據庫 1.1 MySQL優缺點分析 優點 開源免費 社區版完全免費,適合預算有限的企業 允許修改源碼定制功能(需遵守GPL協議) 跨平臺兼容性 支持Windows/Linux/macOS,適配混合環境部署 云服務商…

Pycharm 代理配置

Pycharm 代理配置 文章目錄 Pycharm 代理配置1. 設置系統代理1.1 作用范圍1.2 使用場景1.3 設置步驟 2. 設置 python 運行/調試代理2.1 作用范圍2.2 使用場景2.3 設置步驟 Pycharm 工具作為一款強大的 IDE,其代理配置在實際開發中也是必不可少的,下面介紹…

maven打包時配置多環境參數

1. pom配置 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org/POM/4.…

國產三維CAD皇冠CAD在機械及汽車零部件設計建模教程:斜滑動軸承

在線解讀『斜滑動軸承』的三維建模流程&#xff0c;講解布爾運算、旋轉凸臺/基體、異型導向孔、裝飾螺紋線等操作技巧&#xff0c;一起來皇冠CAD&#xff08;CrownCAD&#xff09;直播間學習制作步驟吧&#xff01; 斜滑動軸承憑借其獨特的工作原理和廣泛的應用領域&#xff0c…

linux(操作系統概述和虛擬機的安裝)

1.操作系統 一、主流服務器操作系統 Windows server 市場地位&#xff1a;適合傳統企業&#xff08;主要以中小型企業、金融機構和教育機構為主&#xff09; 核心特點&#xff1a; 企業級功能&#xff1a;活動目錄、組策略、IIS/Web服務器、Exchange郵件服務 易用性&#xff1a…

鴻蒙生態新利器:華為ArkUI-X混合開發框架深度解析

鴻蒙生態新利器&#xff1a;華為ArkUI-X混合開發框架深度解析 作者&#xff1a;王老漢 | 鴻蒙生態開發者 | 2025年4月 &#x1f4e2; 前言&#xff1a;開發者們的新機遇 各位鴻蒙開發者朋友們&#xff0c;是否還在為多平臺開發重復造輪子而苦惱&#xff1f;今天給大家介紹一位…

數據結構初階:二叉樹(四)

概述&#xff1a;本篇博客主要介紹鏈式結構二叉樹的實現。 目錄 1.實現鏈式結構二叉樹 1.1 二叉樹的頭文件&#xff08;tree.h&#xff09; 1.2 創建二叉樹 1.3 前中后序遍歷 1.3.1 遍歷規則 1.3.1.1 前序遍歷代碼實現 1.3.1.2 中序遍歷代碼實現 1.3.1.3 后序遍歷代…

Electron Forge【實戰】桌面應用 —— AI聊天(下)

此為系列教程&#xff0c;需先完成 Electron Forge【實戰】桌面應用 —— AI聊天&#xff08;上&#xff09;Electron Forge【實戰】桌面應用 —— AI聊天&#xff08;中&#xff09; 會話列表按更新時間倒序加載 src/db.ts db.version(1).stores({// 主鍵為id&#xff0c;且…

[架構之美]Ubuntu源碼部署APISIX全流程詳解(含避坑指南)

[架構之美]Ubuntu源碼部署APISIX全流程詳解(含避坑指南) 一、離線安裝場景需求分析 1.1 典型應用場景 金融/政務內網環境生產環境安全合規要求邊緣計算節點部署1.2 離線安裝難點 #mermaid-svg-B25djI0XquaOb1HM {font-family:"trebuchet ms",verdana,arial,sans-s…

多頭注意力(Multi?Head Attention)

1. 多頭注意力&#xff08;Multi?Head Attention&#xff09;原理 設輸入序列表示為矩陣 X ∈ R B L d model X\in\mathbb{R}^{B\times L\times d_{\text{model}}} X∈RBLdmodel?&#xff0c;其中 B B B&#xff1a;批大小&#xff08;batch size&#xff09;&#xff0c…

系列位置效應——AI與思維模型【80】

一、定義 系列位置效應思維模型是指在一系列事物或信息的呈現過程中&#xff0c;人們對于處于系列開頭和結尾部分的項目的記憶效果優于中間部分項目的現象。具體而言&#xff0c;開頭部分的記憶優勢被稱為首因效應&#xff0c;結尾部分的記憶優勢被稱為近因效應。這種效應反映…

MyBatis XML 配置完整示例(含所有核心配置項)

MyBatis XML 配置完整示例&#xff08;含所有核心配置項&#xff09; 1. 完整 mybatis-config.xml 配置文件 <?xml version"1.0" encoding"UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""htt…

電商數據中臺架構:淘寶 API 實時采集與多源數據融合技術拆解

引言 在當今競爭激烈的電商領域&#xff0c;數據已成為企業決策和業務發展的核心驅動力。電商數據中臺能夠整合和管理企業內外部的各種數據&#xff0c;為業務提供有力支持。其中&#xff0c;淘寶 API 實時采集與多源數據融合技術是數據中臺架構中的關鍵部分。本文將深入探討這…

ubuntu22.04部署Snipe-IT

文章目錄 參考鏈接一、寫在前二、安裝操作系統三、安裝 PHP四、下載 Snipe-IT五、安裝依賴六、安裝數據庫并創建用戶七、安裝 Snipe-IT八、安裝 Nginx九、Web 繼續安裝 Snipe-IT補充&#xff1a;20250427補充&#xff1a; 最后 參考鏈接 How to Install Snipe-IT on Ubuntu 22…