# IMPORT FOREIGN SCHEMA

IMPORT FOREIGN SCHEMA — 從外部伺服器匯入資料表定義

### 語法

```
IMPORT FOREIGN SCHEMA remote_schema
    [ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
    FROM SERVER server_name
    INTO local_schema
    [ OPTIONS ( option 'value' [, ... ] ) ]
```

### 說明

IMPORT FOREIGN SCHEMA 會建立外部資料表，這些外部資料表是外部伺服器上現有的資料表。新的外部資料表將由發出命令的使用者所有，並使用正確的欄位定義和選項來建立，以搭配遠端的資料表。

預設情況下，將導入外部伺服器上特定 schema 中存在的所有資料表和檢視表。可以選擇將資料表限制為指定的名單，也可以排除特定的資料表。所有新的外部資料表都會在目標 schema 中建立，該 schema 必須已經存在。

要使用 IMPORT FOREIGN SCHEMA，使用者必須在外部伺服器上具有 USAGE 權限，並在目標 schema 上具有 CREATE 權限。

### Parameters

*`remote_schema`*

The remote schema to import from. The specific meaning of a remote schema depends on the foreign data wrapper in use.

`LIMIT TO (`` `*`table_name`* \[, ...] )

Import only foreign tables matching one of the given table names. Other tables existing in the foreign schema will be ignored.

`EXCEPT (`` `*`table_name`* \[, ...] )

Exclude specified foreign tables from the import. All tables existing in the foreign schema will be imported except the ones listed here.

*`server_name`*

The foreign server to import from.

*`local_schema`*

The schema in which the imported foreign tables will be created.

`OPTIONS (`` `*`option`* '*`value`*' \[, ...] )

Options to be used during the import. The allowed option names and values are specific to each foreign data wrapper.

### 範例

從伺服器 film\_server 上的遠端 schema foreign\_films 匯入資料表定義，然後在本機 schema films 中建立外部資料表：

```
IMPORT FOREIGN SCHEMA foreign_films
    FROM SERVER film_server INTO films;
```

如上所述，但僅導入兩個資料表 actors 和 directors（如果它們存在的話）：

```
IMPORT FOREIGN SCHEMA foreign_films LIMIT TO (actors, directors)
    FROM SERVER film_server INTO films;
```

### 相容性

IMPORT FOREIGN SCHEMA 指令符合 SQL 標準，但 OPTIONS 子句是 PostgreSQL 的延伸功能。

### *`參閱`*

[CREATE FOREIGN TABLE](/13/reference/sql-commands/create-foreign-table.md), [CREATE SERVER](/13/reference/sql-commands/create-server.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.postgresql.tw/13/reference/sql-commands/import-foreign-schema.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
