Skip to content

Commit 996fcb9

Browse files
committed
Feedback first pass
1 parent 30d635b commit 996fcb9

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

articles/postgresql/flexible-server/connect-python.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ ms.author: sunila
88
author: sunilagarwal
99
ms.devlang: python
1010
ms.custom: mvc, mode-api, devx-track-python
11-
ms.date: 01/02/2024
11+
ms.date: 05/29/2024
1212
---
1313

1414
# Quickstart: Use Python to connect and query data in Azure Database for PostgreSQL - Flexible Server
1515

1616
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
1717

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 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.
1919

2020
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.
2121

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).
2323

2424
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.
2525

@@ -39,7 +39,7 @@ This article assumes that you're familiar with developing using Python, but you'
3939

4040
## Configure Microsoft Entra integration on the server (passwordless only)
4141

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.
4343

4444
## Prepare your development environment
4545

@@ -50,7 +50,6 @@ Change to a folder where you want to run the code and create and activate a [vir
5050
### [Windows](#tab/cmd)
5151

5252
```cmd
53-
# py -3 uses the global python interpreter. You can also use python3 -m venv .venv.
5453
py -3 -m venv .venv
5554
```
5655
@@ -84,7 +83,7 @@ Install the Python libraries needed to run the code examples.
8483
8584
#### [Passwordless (Recommended)](#tab/passwordless)
8685
87-
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.
8887
8988
```Console
9089
pip install psycopg2
@@ -126,13 +125,13 @@ In this section, you add authentication code to your working directory and perfo
126125

127126
credential = DefaultAzureCredential()
128127
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, credential.get_token("https://ossrdbms-aad.database.windows.net").token, sslmode)
129-
return (conn_string)
128+
return conn_string
130129
```
131130

132131
>[!IMPORTANT]
133132
> The code as-shown is for 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.
134133

135-
1. Get database connection information
134+
1. Get database connection information.
136135

137136
1. In the [Azure portal](https://portal.azure.com/), search for and select your Azure Database for PostgreSQL flexible server name.
138137
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
144143
- `<username>` with your Azure user name; for example. `john@contoso.com`.
145144
- `<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.
146145

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:
148147

149148
```azurecli
150149
az login
@@ -168,10 +167,10 @@ In this section, you add authentication code to your working directory and perfo
168167

169168
# Construct connection string
170169
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
171-
return (conn_string)
170+
return conn_string
172171
```
173172

174-
1. Get database connection information
173+
1. Get database connection information.
175174

176175
1. In the [Azure portal](https://portal.azure.com/), search for and select your Azure Database for PostgreSQL flexible server name.
177176
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
245244

246245
## Read data
247246

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().
249248

250249
```Python
251250
import psycopg2
@@ -271,9 +270,18 @@ cursor.close()
271270
conn.close()
272271
```
273272

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+
274282
## Update data
275283

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.
277285

278286
```Python
279287
import psycopg2

0 commit comments

Comments
 (0)