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
In this quickstart, you connect to an Azure Database for PostgreSQL flexible server instance by using Python. You then use SQL statements to query, insert, update, and delete data in the database from Mac, Ubuntu Linux, and Windows platforms.
18
+
In this quickstart, you connect to an Azure Database for PostgreSQL flexible server instance by using Python. You then use SQL statements to query, insert, update, and delete data in the database from macOS, Ubuntu Linux, and Windows platforms.
19
19
20
20
The steps in this article include two authentication methods: Microsoft Entra authentication and PostgreSQL authentication. The **Passwordless** tab shows the Microsoft Entra authentication and the **Password** tab shows the PostgreSQL authentication.
21
21
22
-
Microsoft Entra authentication is a mechanism for connecting to Azure Database for PostgreSQL using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.
22
+
Microsoft Entra authentication is a mechanism for connecting to Azure Database for PostgreSQL using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management. To learn more, see [Microsoft Entra authentication with Azure Database for PostgreSQL - Flexible Server](./concepts-azure-ad-authentication.md).
23
23
24
24
PostgreSQL authentication uses accounts stored in PostgreSQL. If you choose to use passwords as credentials for the accounts, these credentials will be stored in the `user` table. Because these passwords are stored in PostgreSQL, you need to manage the rotation of the passwords by yourself.
25
25
@@ -39,7 +39,7 @@ This article assumes that you're familiar with developing using Python, but you'
39
39
40
40
## Configure Microsoft Entra integration on the server (passwordless only)
41
41
42
-
If you're following the steps for passwordless authentication, Microsoft Entra authentication must be configured for your server instance, and you must be assigned as a Microsoft Entra admin. Follow the steps in [Configure Microsoft Entra integration](./how-to-configure-sign-in-azure-ad-authentication.md) to ensure that Microsoft Entra authentication is configured and that you're a Microsoft Entra administrator.
42
+
If you're following the steps for passwordless authentication, Microsoft Entra authentication must be configured for your server instance, and you must be assigned as a Microsoft Entra administrator on the server instance. Follow the steps in [Configure Microsoft Entra integration](./how-to-configure-sign-in-azure-ad-authentication.md) to ensure that Microsoft Entra authentication is configured and that you're assigned as a Microsoft Entra administrator on your server instance.
43
43
44
44
## Prepare your development environment
45
45
@@ -50,7 +50,6 @@ Change to a folder where you want to run the code and create and activate a [vir
50
50
### [Windows](#tab/cmd)
51
51
52
52
```cmd
53
-
# py -3 uses the global python interpreter. You can also use python3 -m venv .venv.
54
53
py -3 -m venv .venv
55
54
```
56
55
@@ -84,7 +83,7 @@ Install the Python libraries needed to run the code examples.
Install the [psycopg2](https://pypi.python.org/pypi/psycopg2/) module, which enables connecting to and querying a PostgreSQL database, and the [azure-identity](https://pypi.org/project/azure-identity/) library, which provides Microsoft Entra ID token authentication support across the Azure SDK.
86
+
Install the [psycopg2](https://pypi.python.org/pypi/psycopg2/) module, which enables connecting to and querying a PostgreSQL database, and the [azure-identity](https://pypi.org/project/azure-identity/) library, which provides Microsoft Entra token authentication support across the Azure SDK.
88
87
89
88
```Console
90
89
pip install psycopg2
@@ -126,13 +125,13 @@ In this section, you add authentication code to your working directory and perfo
> The code as-shown isfor demonstration purposes only. It's not suitable for use in production. For example, tokens issued by Microsoft Entra ID have a limited lifetime (24 hours by default). In production code, you need to implement a token refresh policy.
134
133
135
-
1. Get database connection information
134
+
1. Get database connection information.
136
135
137
136
1. In the [Azure portal](https://portal.azure.com/), search forand select your Azure Database for PostgreSQL flexible server name.
138
137
1. On the server's **Overview** page, copy the fully qualified **Server name**. The fully qualified **Server name** is always of the form *\<my-server-name>.postgres.database.azure.com*.
@@ -144,7 +143,7 @@ In this section, you add authentication code to your working directory and perfo
144
143
-`<username>`with your Azure user name; for example. `john@contoso.com`.
145
144
-`<database-name>`with the name of your Azure Database for PostgreSQL flexible server database. A default database named *postgres* was automatically created when you created your server. You can rename that database or create a new database by using SQL commands.
146
145
147
-
1. Sign in to Azure on your workstation. You can sign in using the Azure CLI, PowerShell, or Azure Developer CLI. For example, to sign in via the Azure CLI, enter this command:
146
+
1. Sign in to Azure on your workstation. You can sign in using the Azure CLI, Azure PowerShell, or Azure Developer CLI. For example, to sign in via the Azure CLI, enter this command:
148
147
149
148
```azurecli
150
149
az login
@@ -168,10 +167,10 @@ In this section, you add authentication code to your working directory and perfo
1. In the [Azure portal](https://portal.azure.com/), search forand select your Azure Database for PostgreSQL flexible server name.
177
176
1. On the server's **Overview** page, copy the fully qualified **Server name** and the **Server admin login name**. The fully qualified **Server name** is always of the form *\<my-server-name>.postgres.database.azure.com*.
@@ -245,7 +244,7 @@ Inserted 3 rows of data
245
244
246
245
## Read data
247
246
248
-
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**SELECT** statement to read data. This function accepts a query and returns a result set to iterate over by using cursor.fetchall()
247
+
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**SELECT** statement to read data. This function accepts a query and returns a result set to iterate over by using cursor.fetchall().
249
248
250
249
```Python
251
250
import psycopg2
@@ -271,9 +270,18 @@ cursor.close()
271
270
conn.close()
272
271
```
273
272
273
+
When the code runs successfully, it produces the following output:
274
+
275
+
```output
276
+
Connection established
277
+
Data row = (1, banana, 150)
278
+
Data row = (2, orange, 154)
279
+
Data row = (3, apple, 100)
280
+
```
281
+
274
282
## Update data
275
283
276
-
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**UPDATE** statement to update data.
284
+
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**UPDATE** statement to update data.
0 commit comments