You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Connect with Managed Identity to Azure Database for PostgreSQL
13
13
14
-
This article shows you how to use a user-assigned identity for an Azure Virtual Machine (VM) to access an Azure Database for PostgreSQL server. Managed Service Identities are automatically managed by Azure and enable you to authenticate to services that support Azure AD authentication, without needing to insert credentials into your code.
14
+
You can use both system-assigned and user-assigned managed identities to authenticate to Azure Database for PostgreSQL. This article shows you how to use a system-assigned managed identity for an Azure Virtual Machine (VM) to access an Azure Database for PostgreSQL server. Managed Identities are automatically managed by Azure and enable you to authenticate to services that support Azure AD authentication, without needing to insert credentials into your code.
15
15
16
16
You learn how to:
17
17
- Grant your VM access to an Azure Database for PostgreSQL server
18
-
- Create a user in the database that represents the VM's user-assigned identity
18
+
- Create a user in the database that represents the VM's system-assigned identity
19
19
- Get an access token using the VM identity and use it to query an Azure Database for PostgreSQL server
20
20
- Implement the token retrieval in a C# example application
21
21
@@ -27,39 +27,24 @@ You learn how to:
27
27
- You need an Azure Database for PostgreSQL database server that has [Azure AD authentication](howto-configure-sign-in-aad-authentication.md) configured
28
28
- To follow the C# example, first complete the guide how to [Connect with C#](connect-csharp.md)
29
29
30
-
## Creating a user-assigned managed identity for your VM
30
+
## Creating a system-assigned managed identity for your VM
31
31
32
-
Create an identity in your subscription using the [az identity create](/cli/azure/identity#az_identity_create) command. You can use the same resource group that your virtual machine runs in, or a different one.
32
+
Use [az vm identity assign](/cli/azure/vm/identity/) with the `identity assign` command enable the system-assigned identity to an existing VM:
33
33
34
34
```azurecli-interactive
35
-
az identity create --resource-group myResourceGroup --name myManagedIdentity
35
+
az vm identity assign -g myResourceGroup -n myVm
36
36
```
37
37
38
-
To configure the identity in the following steps, use the [az identity show](/cli/azure/identity#az_identity_show) command to store the identity's resource ID and client ID in variables.
38
+
Retrieve the application ID for the system-assigned managed identity, which you'll need in the next few steps:
39
39
40
40
```azurecli
41
-
# Get resource ID of the user-assigned identity
42
-
resourceID=$(az identity show --resource-group myResourceGroup --name myManagedIdentity --query id --output tsv)
We can now assign the user-assigned identity to the VM with the [az vm identity assign](/cli/azure/vm/identity#az_vm_identity_assign) command:
49
-
50
-
```azurecli
51
-
az vm identity assign --resource-group myResourceGroup --name myVM --identities $resourceID
52
-
```
53
-
54
-
To finish setup, show the value of the Client ID, which you'll need in the next few steps:
55
-
56
-
```bash
57
-
echo$clientID
41
+
# Get the client ID (application ID) of the system-assigned managed identity
42
+
az ad sp list --display-name obs-locdev-wus2 --query [*].appId --out tsv
58
43
```
59
44
60
45
## Creating a PostgreSQL user for your Managed Identity
61
46
62
-
Now, connect as the Azure AD administrator user to your PostgreSQL database, and run the following SQL statements:
47
+
Now, connect as the Azure AD administrator user to your PostgreSQL database, and run the following SQL statements, replacing `CLIENT_ID` with the client ID you retrieved for your system-assigned managed identity:
63
48
64
49
```sql
65
50
SET aad_validate_oids_in_tenant = off;
@@ -96,7 +81,7 @@ You are now connected to the database you've configured earlier.
96
81
97
82
This section shows how to get an access token using the VM's user-assigned managed identity and use it to call Azure Database for PostgreSQL. Azure Database for PostgreSQL natively supports Azure AD authentication, so it can directly accept access tokens obtained using managed identities for Azure resources. When creating a connection to PostgreSQL, you pass the access token in the password field.
98
83
99
-
Here's a .NET code example of opening a connection to PostgreSQL using an access token. This code must run on the VM to access the VM's user-assigned managed identity's endpoint. .NET Framework 4.6 or higher or .NET Core 2.2 or higher is required to use the access token method. Replace the values of HOST, USER, DATABASE, and CLIENT_ID.
84
+
Here's a .NET code example of opening a connection to PostgreSQL using an access token. This code must run on the VM to use the system-assigned managed identityto obtain an access token from Azure AD. Replace the values of HOST, USER, DATABASE, and CLIENT_ID.
100
85
101
86
```csharp
102
87
usingSystem;
@@ -107,6 +92,7 @@ using System.Collections.Generic;
107
92
usingSystem.Text.Json;
108
93
usingSystem.Text.Json.Serialization;
109
94
usingNpgsql;
95
+
usingAzure.Identity;
110
96
111
97
namespaceDriver
112
98
{
@@ -117,31 +103,25 @@ namespace Driver
117
103
privatestaticstringHost="HOST";
118
104
privatestaticstringUser="USER";
119
105
privatestaticstringDatabase="DATABASE";
120
-
privatestaticstringClientId="CLIENT_ID";
106
+
//private static string ClientId = "CLIENT_ID";
121
107
122
-
staticvoidMain(string[] args)
108
+
staticasyncTaskMain(string[] args)
123
109
{
124
110
//
125
111
// Get an access token for PostgreSQL.
126
112
//
127
-
Console.Out.WriteLine("Getting access token from Azure Instance Metadata service...");
113
+
Console.Out.WriteLine("Getting access token from Azure AD...");
128
114
129
115
// Azure AD resource ID for Azure Database for PostgreSQL is https://ossrdbms-aad.database.windows.net/
0 commit comments