【问题标题】:Postgres fdw server relation doesn't existPostgres fdw 服务器关系不存在
【发布时间】:2020-06-11 21:22:50
【问题描述】:

我正在尝试使用 FDW 在本地的 postgres 数据库之间创建链接。这是我创建此链接的​​代码:

CREATE EXTENSION IF NOT EXISTS postgres_fdw;

CREATE SERVER IF NOT EXISTS TEST_SERVER FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '127.0.0.1', dbname 'TestDatabase', port '5432');

CREATE USER MAPPING IF NOT EXISTS FOR postgres SERVER TEST_SERVER OPTIONS (user 'postgres', password 'postgres');

CREATE FOREIGN TABLE IF NOT EXISTS TEST_PUBLIC.TEST_TABLE(
                ID INT NOT NULL,
                VALUE VARCHAR(11) NOT NULL
) SERVER TEST_SERVER;    

TestDatabase 上存在 TEST 表,并且我正在创建 FDW 的当前数据库上存在 test_public 模式。当我最初创建并测试它时,我可以看到没有问题的对象,但是我的开发过程的一部分(对于这个特定项目)是删除当前的所有对象并从头开始重新运行,所以当我们将它移动到我们知道从头到尾的新环境一切都很稳固。

我遇到的问题是我无法再看到外部表“TEST_TABLE”。

当我运行这个时:

SELECT * FROM information_schema.tables WHERE table_schema = 'test_public'

我得到以下返回:

table_catalog table_schema  table_name  table_type
TestDatabase  test_public   test_table  FOREIGN

但是当我从表 test_public.test_table 中选择时,我得到以下错误:

relation "test_public.test_table" does not exist 

在此过程中,我能够弄清楚我的所有问题,但即使在逐步运行此操作而不是完整部署之后,我似乎也无法再访问外部表了。关于我缺少什么的任何建议?

【问题讨论】:

    标签: postgresql postgres-fdw


    【解决方案1】:

    您需要使用 OPTIONS 子句将源表映射到外部表 (见details)。

    在您的情况下,它应该类似于(假设源表在 public 架构中名为 test):

    create foreign table test_public.test_table(
    id int not null, 
    value varchar(11) not null) 
    server test_server 
    options(schema_name 'public', table_name 'test');
    

    这是一个改编自您的代码的示例,也使用本地实例。

    这里是源代码:

    create database source_db;
    \c source_db 
    create table source_table(c int); 
    insert into source_table values(12);
    --
    \c postgres
    create schema target_schema;
    create server target_server foreign data wrapper postgres_fdw
    options (host '127.0.0.1', dbname 'source_db', port '5431');
    create user mapping for postgres server target_server 
    options (user 'postgres', password 'postgres'); 
    create foreign table target_schema.target_table(c int) server target_server 
    options(schema_name 'public', table_name 'source_table');
    select * from target_schema.target_table;
    

    这里是执行:

    create database source_db;
    CREATE DATABASE
    
    You are now connected to database "source_db" as user "postgres".
    
    create table source_table(c int);
    CREATE TABLE
    
    insert into source_table values(12);
    INSERT 0 1
    
    You are now connected to database "postgres" as user "postgres".
    
    create schema target_schema;
    CREATE SCHEMA
    
    create server target_server foreign data wrapper postgres_fdw
    options (host '127.0.0.1', dbname 'source_db', port '5431');
    CREATE SERVER
    
    create user mapping for postgres server target_server 
    options (user 'postgres', password 'postgres');
    CREATE USER MAPPING
    
    create foreign table target_schema.target_table(c int) server target_server 
    options(schema_name 'public', table_name 'source_table');
    CREATE FOREIGN TABLE
    
    select * from target_schema.target_table;
     c  
    ----
     12
    (1 row)
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2020-04-09
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2015-08-13
      • 1970-01-01
      • 2020-01-29
      相关资源
      最近更新 更多