Skip to content

Commit 30d635b

Browse files
committed
Add get connection method to the other samples and add note about token refresh.
1 parent c89350b commit 30d635b

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ In this section, you add authentication code to your working directory and perfo
129129
return (conn_string)
130130
```
131131

132+
>[!IMPORTANT]
133+
> 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.
134+
132135
1. Get database connection information
133136

134137
1. In the [Azure portal](https://portal.azure.com/), search for and select your Azure Database for PostgreSQL flexible server name.
@@ -246,16 +249,10 @@ The following code example connects to your Azure Database for PostgreSQL flexib
246249

247250
```Python
248251
import psycopg2
252+
from get_conn_str import get_connection_string
249253

250-
# Update connection string information
251-
host = "<server-name>"
252-
dbname = "<database-name>"
253-
user = "<admin-username>"
254-
password = "<admin-password>"
255-
sslmode = "require"
254+
conn_string = get_connection_string()
256255

257-
# Construct connection string
258-
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
259256
conn = psycopg2.connect(conn_string)
260257
print("Connection established")
261258
cursor = conn.cursor()
@@ -280,16 +277,10 @@ The following code example connects to your Azure Database for PostgreSQL flexib
280277

281278
```Python
282279
import psycopg2
280+
from get_conn_str import get_connection_string
283281

284-
# Update connection string information
285-
host = "<server-name>"
286-
dbname = "<database-name>"
287-
user = "<admin-username>"
288-
password = "<admin-password>"
289-
sslmode = "require"
282+
conn_string = get_connection_string()
290283

291-
# Construct connection string
292-
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
293284
conn = psycopg2.connect(conn_string)
294285
print("Connection established")
295286
cursor = conn.cursor()
@@ -310,16 +301,10 @@ The following code example connects to your Azure Database for PostgreSQL flexib
310301

311302
```Python
312303
import psycopg2
304+
from get_conn_str import get_connection_string
313305

314-
# Update connection string information
315-
host = "<server-name>"
316-
dbname = "<database-name>"
317-
user = "<admin-username>"
318-
password = "<admin-password>"
319-
sslmode = "require"
306+
conn_string = get_connection_string()
320307

321-
# Construct connection string
322-
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
323308
conn = psycopg2.connect(conn_string)
324309
print("Connection established")
325310
cursor = conn.cursor()

0 commit comments

Comments
 (0)