使用 Azure 数据工厂从 PostgreSQL 复制数据Copy data from PostgreSQL by using Azure Data Factory

适用于: Azure 数据工厂 Azure Synapse Analytics

本文概述了如何使用 Azure 数据工厂中的复制活动从 PostgreSQL 数据库复制数据。This article outlines how to use the Copy Activity in Azure Data Factory to copy data from a PostgreSQL database. 它是基于概述复制活动总体的复制活动概述一文。It builds on the copy activity overview article that presents a general overview of copy activity.

支持的功能Supported capabilities

以下活动支持此 PostgreSQL 连接器:This PostgreSQL connector is supported for the following activities:

可以将数据从 PostgreSQL 数据库复制到任何支持的接收器数据存储。You can copy data from PostgreSQL database to any supported sink data store. 有关复制活动支持作为源/接收器的数据存储列表,请参阅支持的数据存储表。For a list of data stores that are supported as sources/sinks by the copy activity, see the Supported data stores table.

具体而言,此 PostgreSQL 连接器支持 PostgreSQL 7.4 及更高版本 。Specifically, this PostgreSQL connector supports PostgreSQL version 7.4 and above.

先决条件Prerequisites

如果数据存储位于本地网络、Azure 虚拟网络或 Amazon Virtual Private Cloud 内部,则需要配置自承载集成运行时才能连接到该数据存储。If your data store is located inside an on-premises network, an Azure virtual network, or Amazon Virtual Private Cloud, you need to configure a self-hosted integration runtime to connect to it.

另外,如果数据存储是托管的云数据服务,可以使用 Azure 集成运行时。Alternatively, if your data store is a managed cloud data service, you can use Azure integration runtime. 如果访问范围限制为防火墙规则中允许的 IP,你可以选择将 Azure Integration Runtime IP 添加到允许列表。If the access is restricted to IPs that are approved in the firewall rules, you can add Azure Integration Runtime IPs into the allow list.

要详细了解网络安全机制和数据工厂支持的选项,请参阅数据访问策略For more information about the network security mechanisms and options supported by Data Factory, see Data access strategies.

集成运行时从版本 3.7 开始提供内置 PostgreSQL 驱动程序,因此无需手动安装任何驱动程序。The Integration Runtime provides a built-in PostgreSQL driver starting from version 3.7, therefore you don't need to manually install any driver.

入门Getting started

若要使用管道执行复制活动,可以使用以下工具或 SDK 之一:To perform the Copy activity with a pipeline, you can use one of the following tools or SDKs:

对于特定于 PostgreSQL 连接器的数据工厂实体,以下部分提供有关用于定义这些实体的属性的详细信息。The following sections provide details about properties that are used to define Data Factory entities specific to PostgreSQL connector.

链接服务属性Linked service properties

PostgreSQL 链接的服务支持以下属性:The following properties are supported for PostgreSQL linked service:

属性Property 说明Description 必须Required
typetype type 属性必须设置为:PostgreSqlThe type property must be set to: PostgreSql Yes
connectionStringconnectionString 用于连接到 Azure Database for PostgreSQL 的 ODBC 连接字符串。An ODBC connection string to connect to Azure Database for PostgreSQL.
还可以将密码放在 Azure 密钥保管库中,并从连接字符串中拉取 password 配置。You can also put password in Azure Key Vault and pull the password configuration out of the connection string. 有关更多详细信息,请参阅以下示例和在 Azure 密钥保管库中存储凭据一文。Refer to the following samples and Store credentials in Azure Key Vault article with more details.
Yes
connectViaconnectVia 用于连接到数据存储的集成运行时The Integration Runtime to be used to connect to the data store. 先决条件部分了解更多信息。Learn more from Prerequisites section. 如果未指定,则使用默认 Azure Integration Runtime。If not specified, it uses the default Azure Integration Runtime. No

典型的连接字符串为 Server=<server>;Database=<database>;Port=<port>;UID=<username>;Password=<Password>A typical connection string is Server=<server>;Database=<database>;Port=<port>;UID=<username>;Password=<Password>. 你可以根据自己的情况设置更多属性:More properties you can set per your case:

属性Property 说明Description 选项Options 必须Required
EncryptionMethod (EM)EncryptionMethod (EM) 驱动程序用于加密在驱动程序和数据库服务器之间发送的数据的方法。The method the driver uses to encrypt data sent between the driver and the database server. 例如,EncryptionMethod=<0/1/6>;E.g., EncryptionMethod=<0/1/6>; 0 (No Encryption) (Default) / 1 (SSL) / 6 (RequestSSL)0 (No Encryption) (Default) / 1 (SSL) / 6 (RequestSSL) No
ValidateServerCertificate (VSC)ValidateServerCertificate (VSC) 启用 SSL 加密后,确定驱动程序是否验证数据库服务器发送的证书(加密方法=1)。Determines whether the driver validates the certificate that is sent by the database server when SSL encryption is enabled (Encryption Method=1). 例如,ValidateServerCertificate=<0/1>;E.g., ValidateServerCertificate=<0/1>; 0 (Disabled) (Default) / 1 (Enabled)0 (Disabled) (Default) / 1 (Enabled) No

示例:Example:

{
    "name": "PostgreSqlLinkedService",
    "properties": {
        "type": "PostgreSql",
        "typeProperties": {
            "connectionString": "Server=<server>;Database=<database>;Port=<port>;UID=<username>;Password=<Password>"
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

示例:在 Azure 密钥保管库中存储密码Example: store password in Azure Key Vault

{
    "name": "PostgreSqlLinkedService",
    "properties": {
        "type": "PostgreSql",
        "typeProperties": {
            "connectionString": "Server=<server>;Database=<database>;Port=<port>;UID=<username>;",
            "password": { 
                "type": "AzureKeyVaultSecret", 
                "store": { 
                    "referenceName": "<Azure Key Vault linked service name>", 
                    "type": "LinkedServiceReference" 
                }, 
                "secretName": "<secretName>" 
            }
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

如果使用具有以下有效负载的 PostgreSQL 链接服务,服务仍按原样受到支持,但建议使用新的版本。If you were using PostgreSQL linked service with the following payload, it is still supported as-is, while you are suggested to use the new one going forward.

先前的有效负载:Previous payload:

{
    "name": "PostgreSqlLinkedService",
    "properties": {
        "type": "PostgreSql",
        "typeProperties": {
            "server": "<server>",
            "database": "<database>",
            "username": "<username>",
            "password": {
                "type": "SecureString",
                "value": "<password>"
            }
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

数据集属性Dataset properties

有关可用于定义数据集的各部分和属性的完整列表,请参阅数据集一文。For a full list of sections and properties available for defining datasets, see the datasets article. 本部分提供 PostgreSQL 数据集支持的属性列表。This section provides a list of properties supported by PostgreSQL dataset.

从 PostgreSQL 复制数据时,支持以下属性:To copy data from PostgreSQL, the following properties are supported:

属性Property 说明Description 必须Required
typetype 数据集的 type 属性必须设置为:PostgreSqlTableThe type property of the dataset must be set to: PostgreSqlTable Yes
架构schema 架构的名称。Name of the schema. 否(如果指定了活动源中的“query”)No (if "query" in activity source is specified)
table 表的名称。Name of the table. 否(如果指定了活动源中的“query”)No (if "query" in activity source is specified)
tableNametableName 具有架构的表的名称。Name of the table with schema. 支持此属性是为了向后兼容。This property is supported for backward compatibility. 对于新的工作负荷,请使用 schematableUse schema and table for new workload. 否(如果指定了活动源中的“query”)No (if "query" in activity source is specified)

示例Example

{
    "name": "PostgreSQLDataset",
    "properties":
    {
        "type": "PostgreSqlTable",
        "typeProperties": {},
        "schema": [],
        "linkedServiceName": {
            "referenceName": "<PostgreSQL linked service name>",
            "type": "LinkedServiceReference"
        }
    }
}

如果使用 RelationalTable 类型数据集,该数据集仍按原样受支持,但我们建议今后使用新数据集。If you were using RelationalTable typed dataset, it's still supported as-is, while you are suggested to use the new one going forward.

复制活动属性Copy activity properties

有关可用于定义活动的各部分和属性的完整列表,请参阅管道一文。For a full list of sections and properties available for defining activities, see the Pipelines article. 本部分提供 PostgreSQL 源支持的属性列表。This section provides a list of properties supported by PostgreSQL source.

以 PostgreSQL 作为源PostgreSQL as source

从 PostgreSQL 复制数据时,复制活动的 source 节支持以下属性:To copy data from PostgreSQL, the following properties are supported in the copy activity source section:

属性Property 说明Description 必须Required
typetype 复制活动 source 的 type 属性必须设置为:PostgreSqlSourceThe type property of the copy activity source must be set to: PostgreSqlSource Yes
查询query 使用自定义 SQL 查询读取数据。Use the custom SQL query to read data. 例如:"query": "SELECT * FROM \"MySchema\".\"MyTable\""For example: "query": "SELECT * FROM \"MySchema\".\"MyTable\"". 否(如果指定了数据集中的“tableName”)No (if "tableName" in dataset is specified)

备注

架构和表名称区分大小写。Schema and table names are case-sensitive. 在查询中将名称括在 ""(双引号)中。Enclose them in "" (double quotes) in the query.

示例:Example:

"activities":[
    {
        "name": "CopyFromPostgreSQL",
        "type": "Copy",
        "inputs": [
            {
                "referenceName": "<PostgreSQL input dataset name>",
                "type": "DatasetReference"
            }
        ],
        "outputs": [
            {
                "referenceName": "<output dataset name>",
                "type": "DatasetReference"
            }
        ],
        "typeProperties": {
            "source": {
                "type": "PostgreSqlSource",
                "query": "SELECT * FROM \"MySchema\".\"MyTable\""
            },
            "sink": {
                "type": "<sink type>"
            }
        }
    }
]

如果使用 RelationalSource 类型源,该源仍按原样受支持,但我们建议今后使用新源。If you were using RelationalSource typed source, it is still supported as-is, while you are suggested to use the new one going forward.

Lookup 活动属性Lookup activity properties

若要了解有关属性的详细信息,请查看 Lookup 活动To learn details about the properties, check Lookup activity.

后续步骤Next steps

有关 Azure 数据工厂中复制活动支持作为源和接收器的数据存储的列表,请参阅支持的数据存储For a list of data stores supported as sources and sinks by the copy activity in Azure Data Factory, see supported data stores.