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/service-connector/tutorial-django-webapp-postgres-cli.md
+2-139Lines changed: 2 additions & 139 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,33 +7,15 @@ author: maud-lv
7
7
ms.author: malev
8
8
ms.service: service-connector
9
9
ms.topic: tutorial
10
-
ms.date: 11/22/2023
10
+
ms.date: 05/13/2024
11
11
zone_pivot_groups: postgres-server-options
12
12
---
13
13
# Tutorial: Using Service Connector to build a Django app with Postgres on Azure App Service
14
14
15
15
> [!NOTE]
16
16
> You are using Service Connector that makes it easier to connect your web app to database service in this tutorial. The tutorial here is a modification of the [App Service tutorial](../app-service/tutorial-python-postgresql-app.md) to use this feature so you will see similarities. Look into section [Configure environment variables to connect the database](#configure-environment-variables-to-connect-the-database) in this tutorial to see where Service Connector comes into play and simplifies the connection process given in the App Service tutorial.
17
17
18
-
::: zone pivot="postgres-single-server"
19
-
20
-
This tutorial shows how to deploy a data-driven Python [Django](https://www.djangoproject.com/) web app to [Azure App Service](overview.md) and connect it to an Azure Database for a Postgres database. You can also try the PostgreSQL Flexible Server by selecting the option above. Flexible Server provides a simpler deployment mechanism and lower ongoing costs.
21
-
22
-
In this tutorial, you use the Azure CLI to complete the following tasks:
23
-
24
-
> [!div class="checklist"]
25
-
> * Set up your initial environment with Python and the Azure CLI
26
-
> * Create an Azure Database for PostgreSQL database
27
-
> * Deploy code to Azure App Service and connect to PostgreSQL
28
-
> * Update your code and redeploy
29
-
> * View diagnostic logs
30
-
> * Manage the web app in the Azure portal
31
-
32
-
:::zone-end
33
-
34
-
::: zone pivot="postgres-flexible-server"
35
-
36
-
This tutorial shows how to deploy a data-driven Python [Django](https://www.djangoproject.com/) web app to [Azure App Service](overview.md) and connect it to an [Azure Database for PostgreSQL Flexible server](../postgresql/flexible-server/index.yml) database. If you can't use PostgreSQL Flexible server, then select the Single Server option above.
18
+
This tutorial shows how to deploy a data-driven Python [Django](https://www.djangoproject.com/) web app to [Azure App Service](overview.md) and connect it to an [Azure Database for PostgreSQL Flexible server](../postgresql/flexible-server/index.yml) database.
37
19
38
20
In this tutorial, you'll use the Azure CLI to complete the following tasks:
39
21
@@ -45,8 +27,6 @@ In this tutorial, you'll use the Azure CLI to complete the following tasks:
45
27
> * View diagnostic logs
46
28
> * Manage the web app in the Azure portal
47
29
48
-
:::zone-end
49
-
50
30
## Prerequisites
51
31
52
32
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/).
@@ -95,26 +75,18 @@ Navigate into the following folder:
95
75
cd serviceconnector-webapp-postgresql-django
96
76
```
97
77
98
-
::: zone pivot="postgres-flexible-server"
99
-
100
78
Use the flexible-server branch of the sample, which contains a few necessary changes, such as how the database server URL is set and adding `'OPTIONS': {'sslmode': 'require'}` to the Django database configuration as required by Azure PostgreSQL Flexible server.
For Flexible server, selectthe branches control that says "master" and thenselectthe**flexible-server** branch.
115
89
116
-
::: zone-end
117
-
118
90
Select **Code**, and thenselect**Download ZIP**.
119
91
120
92
Unpack the ZIP file into a folder named *djangoapp*.
@@ -136,64 +108,6 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
136
108
137
109
## Create Postgres database in Azure
138
110
139
-
::: zone pivot="postgres-single-server"
140
-
141
-
<!-- > [!NOTE]
142
-
> Before you create an Azure Database for PostgreSQL server, check which [compute generation](../postgresql/concepts-pricing-tiers.md#compute-generations-and-vcores) is available in your region. -->
143
-
144
-
1. Enable parameters caching with the Azure CLI so you don't need to provide those parameters with every command. (Cached values are saved in the *.azure* folder.)
145
-
146
-
```azurecli
147
-
az config param-persist on
148
-
```
149
-
150
-
1. Install the `db-up` extension for the Azure CLI:
151
-
152
-
```azurecli
153
-
az extension add --name db-up
154
-
```
155
-
156
-
If the `az`command isn't recognized, be sure you have the Azure CLI installed as described in [Set up your initial environment](#set-up-your-initial-environment).
157
-
158
-
1. Create the Postgres database in Azure with the [`az postgres up`](/cli/azure/postgres#az-postgres-up) command:
159
-
160
-
```azurecli
161
-
az postgres up --resource-group ServiceConnector-tutorial-rg --location eastus --sku-name B_Gen5_1 --server-name <postgres-server-name> --database-name pollsdb --admin-user <admin-username> --admin-password <admin-password> --ssl-enforcement Enabled
162
-
```
163
-
164
-
Replace the following placeholder texts with your own data:
165
-
166
-
* **Replace** *`<postgres-server-name>`* with a name that's **unique across all Azure** (the server endpoint becomes `https://<postgres-server-name>.postgres.database.azure.com`). A good pattern is to use a combination of your company name and another unique value.
167
-
168
-
* For *`<admin-username>`* and *`<admin-password>`*, specify credentials to create an administrator user for this Postgres server. The admin username can't be *azure_superuser*, *azure_pg_admin*, *admin*, *administrator*, *root*, *guest*, or *public*. It can't start with *pg_*. The password must contain **8 to 128 characters** from three of the following categories: English uppercase letters, English lowercase letters, numbers (0 through 9), and non-alphanumeric characters (for example, *!*, *#*, *%*). The password can't contain a username.
169
-
* Don't use the `$` character in the username or password. You'll later create environment variables with these values where the `$` character has special meaning within the Linux container used to run Python apps.
170
-
* The `*B_Gen5_1*` (Basic, Gen5, 1 core) [pricing tier](../postgresql/concepts-pricing-tiers.md) used here is the least expensive. For production databases, omit the `--sku-name` argument to use the GP_Gen5_2 (General Purpose, Gen 5, 2 cores) tier instead.
171
-
172
-
This command performs the following actions, which may take a few minutes:
173
-
174
-
* Create a [resource group](../azure-resource-manager/management/overview.md#terminology) called `ServiceConnector-tutorial-rg`, if it doesn't already exist.
175
-
* Create a Postgres server named by the `--server-name` argument.
176
-
* Create an administrator account using the `--admin-user` and `--admin-password` arguments. You can omit these arguments to allow the command to generate unique credentials for you.
177
-
* Create a `pollsdb` database as named by the `--database-name` argument.
178
-
* Enable access from your local IP address.
179
-
* Enable access from Azure services.
180
-
* Create a database user with access to the `pollsdb` database.
181
-
182
-
You can do all the steps separately with other `az postgres` and `psql` commands, but `az postgres up` does all the steps together.
183
-
184
-
When the command completes, it outputs a JSON object that contains different connection strings for the database along with the server URL, a generated user name (such as "joyfulKoala@msdocs-djangodb-12345"), and a GUID password.
185
-
186
-
> [!IMPORTANT]
187
-
> Copy the user name and password to a temporary text file as you will need them later in this tutorial.
188
-
189
-
<!-- not all locations support az postgres up -->
190
-
> [!TIP]
191
-
> `-l <location-name>` can be set to any [Azure regions](https://azure.microsoft.com/global-infrastructure/regions/). You can get the regions available to your subscription with the [`az account list-locations`](/cli/azure/account#az-account-list-locations) command. For production apps, put your database and your app in the same location.
192
-
193
-
::: zone-end
194
-
195
-
::: zone pivot="postgres-flexible-server"
196
-
197
111
1. Enable parameters caching with the Azure CLI so you don't need to provide those parameters with every command. (Cached values are saved in the *.azure* folder.)
198
112
199
113
```azurecli
@@ -227,8 +141,6 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
227
141
228
142
1. When the command completes, **copy the command's JSON output to a file** as you need values from the output later in this tutorial, specifically the host, username, and password, along with the database name.
229
143
230
-
::: zone-end
231
-
232
144
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
233
145
234
146
## Deploy the code to Azure App Service
@@ -237,36 +149,6 @@ In this section, you create app host in App Service app, connect this app to the
237
149
238
150
### Create the App Service app
239
151
240
-
::: zone pivot="postgres-single-server"
241
-
242
-
1. In the terminal, make sure you're in the *djangoapp* repository folder that contains the app code.
243
-
244
-
1. Create an App Service app (the host process) with the [`az webapp up`](/cli/azure/webapp#az-webapp-up) command:
245
-
246
-
```azurecli
247
-
az webapp up --resource-group ServiceConnector-tutorial-rg --location eastus --plan ServiceConnector-tutorial-plan --sku B1 --name <app-name>
248
-
```
249
-
<!-- without --sku creates PremiumV2 plan -->
250
-
251
-
* For the `--location` argument, make sure you use the location that [Service Connector supports](concept-region-support.md).
252
-
***Replace***\<app-name>* with a unique name across all Azure (the server endpoint is `https://<app-name>.azurewebsites.net`). Allowed characters for*\<app-name>* are `A`-`Z`, `0`-`9`, and `-`. A good pattern is to use a combination of your company name and an app identifier.
253
-
254
-
This command performs the following actions, which may take a few minutes:
255
-
256
-
<!-- - Create the resource group if it doesn't exist. `--resource-group` is optional. -->
257
-
<!-- No it doesn't. az webapp up doesn't respect --resource-group -->
258
-
259
-
* Create the [resource group](../azure-resource-manager/management/overview.md#terminology) if it doesn't already exist. (In this command you use the same resource group in which you created the database earlier.)
260
-
* Create the [App Service plan](../app-service/overview-hosting-plans.md) *DjangoPostgres-tutorial-plan*in the Basic pricing tier (B1), if it doesn't exist. `--plan` and `--sku` are optional.
261
-
* Create the App Service app if it doesn't exist.
262
-
* Enable default logging for the app, if not already enabled.
263
-
* Upload the repository using ZIP deployment with build automation enabled.
264
-
* Cache common parameters, such as the name of the resource group and App Service plan, into the file *.azure/config*. As a result, you don't need to specify all the same parameter with later commands. For example, to redeploy the app after making changes, you can just run `az webapp up` again without any parameters. Commands that come from CLI extensions, such as `az postgres up`, however, do not at present use the cache, which is why you needed to specify the resource group and location here with the initial use of `az webapp up`.
265
-
266
-
::: zone-end
267
-
268
-
::: zone pivot="postgres-flexible-server"
269
-
270
152
1. In the terminal, make sure you're in the *djangoapp* repository folder that contains the app code.
271
153
272
154
1. Switch to the sample app's `flexible-server` branch. This branch contains specific configuration needed for PostgreSQL Flexible server:
@@ -291,8 +173,6 @@ In this section, you create app host in App Service app, connect this app to the
291
173
* Enable default logging for the app.
292
174
* Upload the repository using ZIP deployment with build automation enabled.
293
175
294
-
::: zone-end
295
-
296
176
Upon successful deployment, the command generates JSON output like the following example:
297
177
298
178
:::image type="content" source="../app-service/media/tutorial-python-postgresql-app/az-webapp-up-output.png" alt-text="Screenshot of the terminal, showing an example output for the az webapp up command." :::
@@ -307,21 +187,6 @@ The app code expects to find database information in four environment variables
307
187
308
188
To set environment variables in App Service, create "app settings" with the following `az connection create` command.
309
189
310
-
::: zone pivot="postgres-single-server"
311
-
312
-
```azurecli
313
-
az webapp connection create postgres --client-type django
314
-
```
315
-
316
-
The resource group, app name, db name are drawn from the cached values. You need to provide admin password of your postgres database during the execution of this command.
317
-
318
-
* The command creates settings named "AZURE_POSTGRESQL_HOST", "AZURE_POSTGRESQL_NAME", "AZURE_POSTGRESQL_USER", "AZURE_POSTGRESQL_PASS" as expected by the app code.
319
-
* If you forgot your admin credentials, the command would guide you to reset it.
320
-
321
-
::: zone-end
322
-
323
-
::: zone pivot="postgres-flexible-server"
324
-
325
190
```azurecli
326
191
az webapp connection create postgres-flexible --client-type django
327
192
```
@@ -331,8 +196,6 @@ The resource group, app name, db name are drawn from the cached values. You need
331
196
* The command creates settings named "AZURE_POSTGRESQL_HOST", "AZURE_POSTGRESQL_NAME", "AZURE_POSTGRESQL_USER", "AZURE_POSTGRESQL_PASS" as expected by the app code.
332
197
* If you forgot your admin credentials, the command would guide you to reset it.
333
198
334
-
::: zone-end
335
-
336
199
> [!NOTE]
337
200
> If you see the error message "The subscription is not registered to use Microsoft.ServiceLinker", please run `az provider register -n Microsoft.ServiceLinker` to register the Service Connector resource provider and run the connection command again.
0 commit comments