首页 > 解决方案 > postgres_fdw 扩展没有解释正确的数据类型

问题描述

我正在尝试收集一些有关 ETL 流程的经验,因此我正在寻找将数据从一个地方传输到另一个地方的方法。现在,我想更好地掌握 postgres_fdw 扩展,因为如果我理解正确,它允许我用一个命令导入整个模式。

IMPORT FOREIGN SCHEMA "public"
    FROM SERVER dvdrental into "public";

我已经按照整个文档安装和使用 fwd 扩展以及从其他表手动导入工作正常。但是,当我尝试这种“批​​量导入”时,对于一个特定的表,我收到以下错误:

ERROR:  type "public.year" does not exist
LINE 5:   release_year public.year OPTIONS (column_name 'release_yea...
                       ^
QUERY:  CREATE FOREIGN TABLE film (
  film_id integer OPTIONS (column_name 'film_id') NOT NULL,
  title character varying(255) OPTIONS (column_name 'title') COLLATE pg_catalog."default" NOT NULL,
  description text OPTIONS (column_name 'description') COLLATE pg_catalog."default",
  release_year public.year OPTIONS (column_name 'release_year'),
  language_id smallint OPTIONS (column_name 'language_id') NOT NULL,
  rental_duration smallint OPTIONS (column_name 'rental_duration') NOT NULL,
  rental_rate numeric(4,2) OPTIONS (column_name 'rental_rate') NOT NULL,
  length smallint OPTIONS (column_name 'length'),
  replacement_cost numeric(5,2) OPTIONS (column_name 'replacement_cost') NOT NULL,
  rating public.mpaa_rating OPTIONS (column_name 'rating'),
  last_update timestamp without time zone OPTIONS (column_name 'last_update') NOT NULL,
  special_features text[] OPTIONS (column_name 'special_features') COLLATE pg_catalog."default",
  fulltext tsvector OPTIONS (column_name 'fulltext') NOT NULL
) SERVER dvdrental
OPTIONS (schema_name 'public', table_name 'film');
CONTEXT:  importing foreign table "film"
SQL state: 42704

当我查看第 5 行时,我明白为什么 postgres 会抛出错误。它试图创建类型为public.year 的列release_year。但是,这不是一个有效的类型(如 INTEGER),因此我得到了错误。

不过,我该如何解决?我的意思是,我怎样才能覆盖 postgres 试图为这个特定列设置的数据类型?

标签: postgresqlpostgres-fdw

解决方案


该表对列film使用域,对year列使用枚举rating

IMPORT FOREIGN SCHEMA只导入表和视图,所以它不导入域、枚举和其他东西。

您需要在导入架构之前手动创建它们。

CREATE TYPE public.mpaa_rating AS ENUM (
    'G',
    'PG',
    'PG-13',
    'R',
    'NC-17'
);

CREATE DOMAIN public.year AS integer
    CONSTRAINT year_check CHECK (((VALUE >= 1901) AND (VALUE <= 2155)));

推荐阅读