Files
Neo-ZQYY/db/fdw/setup_fdw_test.sql

64 lines
3.3 KiB
SQL
Raw Permalink 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 映射配置(测试环境)— 在 test_zqyy_app 数据库中执行
-- 用途:通过 postgres_fdw 将 test_etl_feiqiu.app schema 只读映射到 test_zqyy_app
-- 使业务后端在测试环境下无需直接连接 ETL 测试库即可读取汇总/维度数据。
-- 前提test_etl_feiqiu 数据库已部署 app schema 及 app_reader 角色
-- 基于 setup_fdw.sql仅将目标库替换为测试库
-- =============================================================================
-- -----------------------------------------------------------------------------
-- 1. 安装 postgres_fdw 扩展
-- -----------------------------------------------------------------------------
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
-- -----------------------------------------------------------------------------
-- 2. 创建外部服务器(指向 test_etl_feiqiu 测试库)
-- 部署时按实际环境替换 host / port
-- -----------------------------------------------------------------------------
CREATE SERVER IF NOT EXISTS test_etl_feiqiu_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'test_etl_feiqiu', port '5432');
-- -----------------------------------------------------------------------------
-- 3. 创建用户映射(只读角色)
-- app_user = test_zqyy_app 侧的应用连接角色
-- app_reader = test_etl_feiqiu 侧的只读角色
-- 密码占位符 '***',部署时替换为真实凭据
-- -----------------------------------------------------------------------------
CREATE USER MAPPING IF NOT EXISTS FOR app_user
SERVER test_etl_feiqiu_server
OPTIONS (user 'app_reader', password '***');
-- -----------------------------------------------------------------------------
-- 4. 创建目标 schema存放外部表
-- -----------------------------------------------------------------------------
CREATE SCHEMA IF NOT EXISTS fdw_etl;
-- -----------------------------------------------------------------------------
-- 5. 导入 test_etl_feiqiu.app schema 的所有外部表到 fdw_etl
-- -----------------------------------------------------------------------------
IMPORT FOREIGN SCHEMA app
FROM SERVER test_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 test_etl_feiqiu_server;
-- DROP SERVER IF EXISTS test_etl_feiqiu_server CASCADE;
-- DROP EXTENSION IF EXISTS postgres_fdw;