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
@@ -125,11 +128,33 @@ In this section, you add authentication code to your working directory and perfo
125
128
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*.
126
129
1. On the left menu, under **Security**, select **Authentication**. Make sure your account is listed under **Microsoft Entra Admins**. If it isn't, complete the steps in [Configure Microsoft Entra integration on the server (passwordless only)](#configure-microsoft-entra-integration-on-the-server-passwordless-only).
127
130
128
-
1. Replace the following placeholder values in the code:
131
+
1. Set enviornment variables for the connection URI elements:
132
+
133
+
### [Windows](#tab/cmd)
134
+
135
+
```cmd
136
+
setDBHOST=<server-name>
137
+
setDBNAME=<database-name>
138
+
setDBUSER=<username>
139
+
setSSLMODE=require
140
+
```
141
+
142
+
### [macOS/Linux](#tab/bash)
129
143
130
-
-`<server-name>`with the value you copied from the Azure portal.
131
-
-`<username>`with your Azure user name; for example. `john@contoso.com`.
132
-
-`<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.
144
+
```bash
145
+
DBHOST=<server-name>
146
+
DBNAME=<database-name>
147
+
DBUSER=<username>
148
+
SSLMODE=require
149
+
```
150
+
151
+
---
152
+
153
+
Replace the following placeholder values in the commands:
154
+
155
+
*`<server-name>`with the value you copied from the Azure portal.
156
+
*`<username>`with your Azure user name; for example. `john@contoso.com`.
157
+
*`<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 use that database or create a new database by using SQL commands.
133
158
134
159
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:
135
160
@@ -141,26 +166,24 @@ In this section, you add authentication code to your working directory and perfo
141
166
142
167
#### [Password](#tab/password)
143
168
144
-
1. Copy the following code into an editor and save it in a file named *get_conn_str.py*.
169
+
1. Copy the following code into an editor and save it in a file named *get_conn.py*.
145
170
146
171
```python
172
+
import urllib.parse
173
+
import os
147
174
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 use some
150
-
# other mechanism, like environment variables or Azure keyvault to hold passwords.
@@ -172,11 +195,35 @@ In this section, you add authentication code to your working directory and perfo
172
195
173
196
<!---->
174
197
175
-
1. Replace the following placeholder values in the code:
198
+
1. Set enviornment variables for the connection URI elements:
199
+
200
+
### [Windows](#tab/cmd)
201
+
202
+
```cmd
203
+
setDBHOST=<server-name>
204
+
setDBNAME=<database-name>
205
+
setDBUSER=<username>
206
+
setDBPASSWORD=<password>
207
+
setSSLMODE=require
208
+
```
209
+
210
+
### [macOS/Linux](#tab/bash)
211
+
212
+
```bash
213
+
DBHOST=<server-name>
214
+
DBNAME=<database-name>
215
+
DBUSER=<username>
216
+
DBPASSWORD=<password>
217
+
SSLMODE=require
218
+
```
219
+
220
+
---
221
+
222
+
Replace the following placeholder values in the commands:
176
223
177
-
-`<server-name>`and`<username>`with the values you copied from the Azure portal.
178
-
-`<password>`with your server password.
179
-
-`<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.
224
+
*`<server-name>`and`<username>`with the values you copied from the Azure portal.
225
+
*`<password>`with your server password.
226
+
*`<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.
180
227
181
228
---
182
229
@@ -198,9 +245,9 @@ The following code example connects to your Azure Database for PostgreSQL flexib
198
245
199
246
```Python
200
247
import psycopg2
201
-
fromget_conn_strimportget_connection_string
248
+
fromget_connimportget_connection_uri
202
249
203
-
conn_string = get_connection_string()
250
+
conn_string = get_connection_uri()
204
251
205
252
conn = psycopg2.connect(conn_string)
206
253
print("Connection established")
@@ -241,9 +288,9 @@ The following code example connects to your Azure Database for PostgreSQL flexib
241
288
242
289
```Python
243
290
import psycopg2
244
-
fromget_conn_strimportget_connection_string
291
+
fromget_connimportget_connection_uri
245
292
246
-
conn_string = get_connection_string()
293
+
conn_string = get_connection_uri()
247
294
248
295
conn = psycopg2.connect(conn_string)
249
296
print("Connection established")
@@ -278,9 +325,9 @@ The following code example connects to your Azure Database for PostgreSQL flexib
278
325
279
326
```Python
280
327
import psycopg2
281
-
fromget_conn_strimportget_connection_string
328
+
fromget_connimportget_connection_uri
282
329
283
-
conn_string = get_connection_string()
330
+
conn_string = get_connection_uri()
284
331
285
332
conn = psycopg2.connect(conn_string)
286
333
print("Connection established")
@@ -298,13 +345,13 @@ conn.close()
298
345
299
346
## Delete data
300
347
301
-
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**DELETE** statement to delete an inventory item that you previously inserted.
348
+
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL**DELETE** statement to delete an inventory item that you previously inserted.
0 commit comments