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
Copy file name to clipboardExpand all lines: articles/postgresql/flexible-server/connect-python.md
+41-48Lines changed: 41 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,37 +45,23 @@ If you're following the steps for passwordless authentication, Microsoft Entra a
45
45
46
46
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.
47
47
48
-
1. Create the virtual environment:
48
+
Run the following commands to create and activate a virtual environment:
49
49
50
-
### [Windows](#tab/cmd)
50
+
### [Windows](#tab/cmd)
51
51
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
+
```
71
56
72
-
### [macOS/Linux](#tab/bash)
57
+
### [macOS/Linux](#tab/bash)
73
58
74
-
```bash
75
-
source .venv/bin/activate
76
-
```
59
+
```bash
60
+
python3 -m venv .venv
61
+
source .venv/bin/activate
62
+
```
77
63
78
-
---
64
+
---
79
65
80
66
## Install the Python libraries
81
67
@@ -111,25 +97,27 @@ In this section, you add authentication code to your working directory and perfo
111
97
```python
112
98
from azure.identity import DefaultAzureCredential
113
99
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
+
120
104
defget_connection_string():
121
105
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.
124
114
# Note the requested scope parameter in the call to get_token, "https://ossrdbms-aad.database.windows.net".
> 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.
120
+
```
133
121
134
122
1. Get database connection information.
135
123
@@ -156,17 +144,22 @@ In this section, you add authentication code to your working directory and perfo
156
144
1. Copy the following code into an editor and save it in a file named *get_conn_str.py*.
157
145
158
146
```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.
0 commit comments