Files
Neo-ZQYY/docs/database/ddl/fdw.sql
2026-03-15 10:15:02 +08:00

72 lines
3.7 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================================
-- FDW 跨库映射(在 zqyy_app 中执行)
-- 生成日期2026-03-15
-- 来源db/fdw/setup_fdw.sql
-- =============================================================================
-- =============================================================================
-- FDW 映射配置(生产环境)— 在 zqyy_app 数据库中执行
-- 用途:通过 postgres_fdw 将 etl_feiqiu.app schema 只读映射到 zqyy_app
-- 使业务后端无需直接连接 ETL 数据库即可读取汇总/维度数据。
-- 前提etl_feiqiu 数据库已部署 app schema 及 app_reader 角色(见 app.sql
-- 测试环境版本setup_fdw_test.sql指向 test_etl_feiqiu / test_zqyy_app
-- Requirements: 8.3, 8.4, 8.5
-- =============================================================================
-- -----------------------------------------------------------------------------
-- 1. 安装 postgres_fdw 扩展
-- -----------------------------------------------------------------------------
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
-- -----------------------------------------------------------------------------
-- 2. 创建外部服务器(指向 etl_feiqiu 数据库)
-- 部署时按实际环境替换 host / port
-- -----------------------------------------------------------------------------
CREATE SERVER IF NOT EXISTS etl_feiqiu_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'etl_feiqiu', port '5432');
-- -----------------------------------------------------------------------------
-- 3. 创建用户映射(只读角色)
-- app_user = zqyy_app 侧的应用连接角色
-- app_reader = etl_feiqiu 侧的只读角色(在 app.sql 中已创建)
-- 密码占位符 '***',部署时替换为真实凭据
-- -----------------------------------------------------------------------------
CREATE USER MAPPING IF NOT EXISTS FOR app_user
SERVER etl_feiqiu_server
OPTIONS (user 'app_reader', password '***');
-- -----------------------------------------------------------------------------
-- 4. 创建目标 schema存放外部表
-- -----------------------------------------------------------------------------
CREATE SCHEMA IF NOT EXISTS fdw_etl;
-- -----------------------------------------------------------------------------
-- 5. 导入 etl_feiqiu.app schema 的所有外部表到 fdw_etl
-- 映射为只读zqyy_app 不存储 ETL 数据副本Requirements 8.4
-- -----------------------------------------------------------------------------
IMPORT FOREIGN SCHEMA app
FROM SERVER etl_feiqiu_server
INTO fdw_etl;
-- -----------------------------------------------------------------------------
-- 6. 授权:允许 app_user 访问 fdw_etl schema 及其外部表
-- -----------------------------------------------------------------------------
GRANT USAGE ON SCHEMA fdw_etl TO app_user;
GRANT SELECT ON ALL TABLES IN SCHEMA fdw_etl TO app_user;
-- 未来新导入的外部表自动授权
ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_etl GRANT SELECT ON TABLES TO app_user;
-- =============================================================================
-- 回滚脚本(按逆序执行)
-- =============================================================================
-- ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_etl REVOKE SELECT ON TABLES FROM app_user;
-- REVOKE SELECT ON ALL TABLES IN SCHEMA fdw_etl FROM app_user;
-- REVOKE USAGE ON SCHEMA fdw_etl FROM app_user;
-- DROP SCHEMA IF EXISTS fdw_etl CASCADE;
-- DROP USER MAPPING IF EXISTS FOR app_user SERVER etl_feiqiu_server;
-- DROP SERVER IF EXISTS etl_feiqiu_server CASCADE;
-- DROP EXTENSION IF EXISTS postgres_fdw;