共用方式為


整合 適用於 PostgreSQL 的 Azure 資料庫 與 Service 連線 or

此頁面會顯示支持的驗證方法和用戶端,並顯示您可以使用Service 連線 or 將 適用於 PostgreSQL 的 Azure 資料庫 連線到其他雲端服務的範例程式代碼。 您仍然可以在沒有使用 Service 連線 or 的情況下,以其他程式設計語言連線到 適用於 PostgreSQL 的 Azure 資料庫。 此頁面也會顯示您在建立服務連線時取得的預設環境變數名稱和值(或 Spring Boot 組態)。

支援的計算服務

服務 連線 or 可用來將下列計算服務連線至 適用於 PostgreSQL 的 Azure 資料庫:

  • Azure App Service
  • Azure Functions
  • Azure 應用程式組態
  • Azure Spring Apps

支援的驗證類型和客戶端類型

下表顯示使用 Service 連線 or 將計算服務連線到 適用於 PostgreSQL 的 Azure 資料庫 支援的驗證方法和客戶端組合。 「是」表示支持組合,而「否」則表示不支援。

用戶端類型 系統指派的受控識別 使用者指派的受控識別 秘密/連接字串 服務主體
.NET Yes .是 .是 Yes
Go (pg) Yes .是 .是 Yes
Java (JDBC) Yes .是 .是 Yes
Java - Spring Boot (JDBC) Yes .是 .是 Yes
Node.js (pg) Yes .是 .是 Yes
PHP (原生) Yes .是 .是 Yes
Python (psycopg2) Yes .是 .是 Yes
Python-Django Yes .是 .是 Yes
Ruby (ruby-pg) Yes .是 .是 Yes
Yes .是 .是 Yes

下表指出支持數據表中所有用戶端類型和驗證方法的組合。 所有客戶端類型都可以使用任何驗證方法來使用 Service 連線 or 連線到 適用於 PostgreSQL 的 Azure 資料庫。

注意

系統指派的受控識別、使用者指派的受控識別和服務主體僅支援 Azure CLI。

默認環境變數名稱或應用程式屬性和範例程序代碼

根據連線的驗證類型和用戶端類型,參考下表中的連線詳細數據和範例程式代碼,以將計算服務連線至 適用於 PostgreSQL 的 Azure 資料庫。 如需命名慣例的詳細資訊,請參閱 Service 連線 or internals 一文。

系統指派的受控識別

默認環境變數名稱 描述 範例值
AZURE_POSTGRESQL_CONNECTIONSTRING .NET PostgreSQL 連接字串 Server=<PostgreSQL-server-name>.postgres.database.azure.com;Database=<database-name>;Port=5432;Ssl Mode=Require;User Id=<username>;

範例指令碼

請參閱下列步驟和程序代碼,以使用系統指派的受控識別連線到 適用於 PostgreSQL 的 Azure 資料庫。

針對 .NET,沒有外掛程式或連結庫可支持無密碼連線。 您可以使用 Azure.Identity 之類的客戶端連結庫,取得受控識別或服務主體的存取令牌。 然後,您可以使用存取令牌作為密碼來連線到資料庫。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。

using Azure.Identity;
using Azure.Core;
using Npgsql;

// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();

// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
//     }
// );

// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);

// Acquire the access token. 
AccessToken accessToken = await sqlServerTokenProvider.GetTokenAsync(
    new TokenRequestContext(scopes: new string[]
    {
        "https://ossrdbms-aad.database.windows.net/.default"
    }));

// Combine the token with the connection string from the environment variables provided by Service Connector.
string connectionString =
    $"{Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING")};Password={accessToken.Token}";

// Establish the connection.
using (var connection = new NpgsqlConnection(connectionString))
{
    Console.WriteLine("Opening connection using access token...");
    connection.Open();
}

接下來,如果您在使用服務連接器之前已在 PostgreSQL 彈性伺服器中建立資料表和序列,則必須以擁有者身分連線,並將權限授與 Service Connector 所建立的 <aad-username>。 服務連接器所設定之連接字串或組態的使用者名稱看起來應該會像 aad_<connection name>。 如果您使用 Azure 入口網站,請選取 Service Type 資料行旁的展開按鈕,並取得值。 如果您使用 Azure CLI,請在 CLI 命令輸出中檢查 configurations

然後,執行查詢以授與權限

az extension add --name rdbms-connect

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO \"<aad-username>\";GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO \"<aad username>\";"

<owner-username><owner-password> 是現有資料表的擁有者,可以授與其他資料表的權限。 <aad-username> 是由服務連接器所建立的使用者。 以實際的值取代它們。

使用命令驗證結果:

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "SELECT distinct(table_name) FROM information_schema.table_privileges WHERE grantee='<aad-username>' AND table_schema='public';" --output table

使用者指派的受控識別

默認環境變數名稱 描述 範例值
AZURE_POSTGRESQL_CLIENTID 您的用戶端識別碼 <identity-client-ID>
AZURE_POSTGRESQL_CONNECTIONSTRING .NET PostgreSQL 連接字串 Server=<PostgreSQL-server-name>.postgres.database.azure.com;Database=<database-name>;Port=5432;Ssl Mode=Require;User Id=<username>;

範例指令碼

請參閱下列步驟和程序代碼,以使用使用者指派的受控識別連線到 適用於 PostgreSQL 的 Azure 資料庫。

針對 .NET,沒有外掛程式或連結庫可支持無密碼連線。 您可以使用 Azure.Identity 之類的客戶端連結庫,取得受控識別或服務主體的存取令牌。 然後,您可以使用存取令牌作為密碼來連線到資料庫。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。

using Azure.Identity;
using Azure.Core;
using Npgsql;

// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();

// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
//     }
// );

// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);

// Acquire the access token. 
AccessToken accessToken = await sqlServerTokenProvider.GetTokenAsync(
    new TokenRequestContext(scopes: new string[]
    {
        "https://ossrdbms-aad.database.windows.net/.default"
    }));

// Combine the token with the connection string from the environment variables provided by Service Connector.
string connectionString =
    $"{Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING")};Password={accessToken.Token}";

// Establish the connection.
using (var connection = new NpgsqlConnection(connectionString))
{
    Console.WriteLine("Opening connection using access token...");
    connection.Open();
}

接下來,如果您在使用服務連接器之前已在 PostgreSQL 彈性伺服器中建立資料表和序列,則必須以擁有者身分連線,並將權限授與 Service Connector 所建立的 <aad-username>。 服務連接器所設定之連接字串或組態的使用者名稱看起來應該會像 aad_<connection name>。 如果您使用 Azure 入口網站,請選取 Service Type 資料行旁的展開按鈕,並取得值。 如果您使用 Azure CLI,請在 CLI 命令輸出中檢查 configurations

然後,執行查詢以授與權限

az extension add --name rdbms-connect

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO \"<aad-username>\";GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO \"<aad username>\";"

<owner-username><owner-password> 是現有資料表的擁有者,可以授與其他資料表的權限。 <aad-username> 是由服務連接器所建立的使用者。 以實際的值取代它們。

使用命令驗證結果:

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "SELECT distinct(table_name) FROM information_schema.table_privileges WHERE grantee='<aad-username>' AND table_schema='public';" --output table

連接字串

默認環境變數名稱 描述 範例值
AZURE_POSTGRESQL_CONNECTIONSTRING .NET PostgreSQL 連接字串 Server=<PostgreSQL-server-name>.postgres.database.azure.com;Database=<database-name>;Port=5432;Ssl Mode=Require;User Id=<username>;

範例指令碼

請參閱下面的步驟和程序代碼,以使用 連接字串 連線到 適用於 PostgreSQL 的 Azure 資料庫。

  1. 安裝相依性。 請遵循指導方針來 安裝 Npgsql
  2. 在程序代碼中,從 Service 連線 or 服務新增的環境變數取得 PostgreSQL 連接字串。 若要設定 PostgreSQL 伺服器的 TSL 組態,請參閱 下列步驟
    using System;
    using Npgsql;
    
    string connectionString = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING");
    using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
    {
        connection.Open();
    }
    

服務主體

默認環境變數名稱 描述 範例值
AZURE_POSTGRESQL_CLIENTID 您的用戶端識別碼 <client-ID>
AZURE_POSTGRESQL_CLIENTSECRET 您的客戶端密碼 <client-secret>
AZURE_POSTGRESQL_TENANTID 您的租用戶識別碼 <tenant-ID>
AZURE_POSTGRESQL_CONNECTIONSTRING .NET PostgreSQL 連接字串 Server=<PostgreSQL-server-name>.postgres.database.azure.com;Database=<database-name>;Port=5432;Ssl Mode=Require;User Id=<username>;

範例指令碼

請參閱下面的步驟和程序代碼,以使用服務主體連線到 適用於 PostgreSQL 的 Azure 資料庫。

針對 .NET,沒有外掛程式或連結庫可支持無密碼連線。 您可以使用 Azure.Identity 之類的客戶端連結庫,取得受控識別或服務主體的存取令牌。 然後,您可以使用存取令牌作為密碼來連線到資料庫。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。

using Azure.Identity;
using Azure.Core;
using Npgsql;

// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();

// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
//     }
// );

// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);

// Acquire the access token. 
AccessToken accessToken = await sqlServerTokenProvider.GetTokenAsync(
    new TokenRequestContext(scopes: new string[]
    {
        "https://ossrdbms-aad.database.windows.net/.default"
    }));

// Combine the token with the connection string from the environment variables provided by Service Connector.
string connectionString =
    $"{Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING")};Password={accessToken.Token}";

// Establish the connection.
using (var connection = new NpgsqlConnection(connectionString))
{
    Console.WriteLine("Opening connection using access token...");
    connection.Open();
}

接下來,如果您在使用服務連接器之前已在 PostgreSQL 彈性伺服器中建立資料表和序列,則必須以擁有者身分連線,並將權限授與 Service Connector 所建立的 <aad-username>。 服務連接器所設定之連接字串或組態的使用者名稱看起來應該會像 aad_<connection name>。 如果您使用 Azure 入口網站,請選取 Service Type 資料行旁的展開按鈕,並取得值。 如果您使用 Azure CLI,請在 CLI 命令輸出中檢查 configurations

然後,執行查詢以授與權限

az extension add --name rdbms-connect

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO \"<aad-username>\";GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO \"<aad username>\";"

<owner-username><owner-password> 是現有資料表的擁有者,可以授與其他資料表的權限。 <aad-username> 是由服務連接器所建立的使用者。 以實際的值取代它們。

使用命令驗證結果:

az postgres flexible-server execute -n <postgres-name> -u <owner-username> -p "<owner-password>" -d <database-name> --querytext "SELECT distinct(table_name) FROM information_schema.table_privileges WHERE grantee='<aad-username>' AND table_schema='public';" --output table

下一步

請遵循下列教學課程,深入瞭解 Service 連線 or。