Skip to content

Commit be2518e

Browse files
committed
Updated code
1 parent 996fcb9 commit be2518e

File tree

1 file changed

+41
-48
lines changed

1 file changed

+41
-48
lines changed

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

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,23 @@ If you're following the steps for passwordless authentication, Microsoft Entra a
4545

4646
Change to a folder where you want to run the code and create and activate a [virtual environment](https://docs.python.org/3/tutorial/venv.html). A virtual environment is a self-contained directory for a particular version of Python plus the other packages needed for that application.
4747

48-
1. Create the virtual environment:
48+
Run the following commands to create and activate a virtual environment:
4949

50-
### [Windows](#tab/cmd)
50+
### [Windows](#tab/cmd)
5151

52-
```cmd
53-
py -3 -m venv .venv
54-
```
55-
56-
### [macOS/Linux](#tab/bash)
57-
58-
```bash
59-
python3 -m venv .venv
60-
```
61-
62-
---
63-
64-
1. Activate the virtual environment:
65-
66-
### [Windows](#tab/cmd)
67-
68-
```cmd
69-
.venv\Scripts\activate
70-
```
52+
```cmd
53+
py -3 -m venv .venv
54+
.venv\Scripts\activate
55+
```
7156

72-
### [macOS/Linux](#tab/bash)
57+
### [macOS/Linux](#tab/bash)
7358

74-
```bash
75-
source .venv/bin/activate
76-
```
59+
```bash
60+
python3 -m venv .venv
61+
source .venv/bin/activate
62+
```
7763

78-
---
64+
---
7965

8066
## Install the Python libraries
8167

@@ -111,25 +97,27 @@ In this section, you add authentication code to your working directory and perfo
11197
```python
11298
from azure.identity import DefaultAzureCredential
11399

114-
# Update connection string information
115-
host = "<server-name>"
116-
dbname = "<database-name>"
117-
user = "<username>"
118-
sslmode = "require"
119-
100+
# IMPORTANT! This code is for demonstration purposes only. It's not suitable for use in production.
101+
# For example, tokens issued by Microsoft Entra ID have a limited lifetime (24 hours by default).
102+
# In production code, you need to implement a token refresh policy.
103+
120104
def get_connection_string():
121105

122-
# Construct connection string using passwordless authentication via DefaultAzureCredential.
123-
# Call get_token() to get a token from Microsft Entra ID and add it as the password in the connection token.
106+
# Update connection string information
107+
host = "<server-name>"
108+
dbname = "<database-name>"
109+
user = "<username>"
110+
sslmode = "require"
111+
112+
# Use passwordless authentication via DefaultAzureCredential.
113+
# Call get_token() to get a token from Microsft Entra ID and add it as the password in the connection string.
124114
# Note the requested scope parameter in the call to get_token, "https://ossrdbms-aad.database.windows.net".
125-
126115
credential = DefaultAzureCredential()
127-
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)
116+
token=credential.get_token("https://ossrdbms-aad.database.windows.net").token
117+
118+
conn_string = f"host={host} user={user} dbname={dbname} password={token} sslmode={sslmode}"
128119
return conn_string
129-
```
130-
131-
>[!IMPORTANT]
132-
> 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.
120+
```
133121

134122
1. Get database connection information.
135123

@@ -156,17 +144,22 @@ In this section, you add authentication code to your working directory and perfo
156144
1. Copy the following code into an editor and save it in a file named *get_conn_str.py*.
157145

158146
```python
159-
# Update connection string information
160-
host = "<server-name>"
161-
dbname = "<database-name>"
162-
user = "<username>"
163-
password = "<password>"
164-
sslmode = "require"
165-
147+
148+
# IMPORTANT! This code is for demonstration purposes only. It's not suitable for use in production.
149+
# For example, in production you should never place a password directly in code. Instead, you should some
150+
# other mechanism, like environment variables or Azure keyvault to hold passwords.
151+
166152
def get_connection_string():
167153

154+
# Update connection string information
155+
host = "<server-name>"
156+
dbname = "<database-name>"
157+
user = "<username>"
158+
password = "<password>"
159+
sslmode = "require"
160+
168161
# Construct connection string
169-
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
162+
conn_string = f"host={host} user={user} dbname={dbname} password={password} sslmode={sslmode}"
170163
return conn_string
171164
```
172165

0 commit comments

Comments
 (0)