Postgresql 中文操作指南
5.12. Foreign Data #
PostgreSQL 实现了部分 SQL/MED 规范,允许你使用常规 SQL 查询访问驻留在 PostgreSQL 之外的的 data。此类数据称为 foreign data。(请注意,不要将此用法与外键混淆后者是数据库中的一种约束类型。)
PostgreSQL implements portions of the SQL/MED specification, allowing you to access data that resides outside PostgreSQL using regular SQL queries. Such data is referred to as foreign data. (Note that this usage is not to be confused with foreign keys, which are a type of constraint within the database.)
借助 _foreign data wrapper_可以访问外部数据。外部 data 封装器是一个库,可以与外部数据源通信,隐藏连接到数据源和从 data 源获取数据时的详细信息。有些外部 data 封装器可用作 _contrib_模块;请参见 Appendix F。其他类型的外部 data 封装器可能作为第三方产品被发现。如果现有的外部 data 封装器都不适合你的需要,你可以编写自己的;请参见 Chapter 59。
Foreign data is accessed with help from a foreign data wrapper. A foreign data wrapper is a library that can communicate with an external data source, hiding the details of connecting to the data source and obtaining data from it. There are some foreign data wrappers available as contrib modules; see Appendix F. Other kinds of foreign data wrappers might be found as third party products. If none of the existing foreign data wrappers suit your needs, you can write your own; see Chapter 59.
要访问外部数据,你需要创建 foreign server_对象,其定义如何根据支持外部 data 封装器所用的一组选项连接到特定的外部 data 源。然后,你需要创建一个或多个 _foreign tables,其定义远程数据结构。外部表可以在查询中像普通表一样使用,但外部表在 PostgreSQL 服务器中没有存储空间。每当使用它时,PostgreSQL 都会要求外部 data 封装器从外部源获取 data,或在更新命令的情况下将数据传输到外部源。
To access foreign data, you need to create a foreign server object, which defines how to connect to a particular external data source according to the set of options used by its supporting foreign data wrapper. Then you need to create one or more foreign tables, which define the structure of the remote data. A foreign table can be used in queries just like a normal table, but a foreign table has no storage in the PostgreSQL server. Whenever it is used, PostgreSQL asks the foreign data wrapper to fetch data from the external source, or transmit data to the external source in the case of update commands.
访问远程数据可能需要向外部 data 源进行身份验证。此信息可以由 _user mapping_提供,它可以基于当前 PostgreSQL 角色提供附加数据(比如用户名和密码)。
Accessing remote data may require authenticating to the external data source. This information can be provided by a user mapping, which can provide additional data such as user names and passwords based on the current PostgreSQL role.
有关更多信息,请参见 CREATE FOREIGN DATA WRAPPER 、 CREATE SERVER 、 CREATE USER MAPPING 、 CREATE FOREIGN TABLE 和 IMPORT FOREIGN SCHEMA 。
For additional information, see CREATE FOREIGN DATA WRAPPER, CREATE SERVER, CREATE USER MAPPING, CREATE FOREIGN TABLE, and IMPORT FOREIGN SCHEMA.