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/app-service/tutorial-python-postgresql-app.md
+26-28Lines changed: 26 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: 'Tutorial: Deploy a Python Django or Flask web app with PostgreSQL'
3
3
description: Create a Python Django or Flask web app with a PostgreSQL database and deploy it to Azure. The tutorial uses either the Django or Flask framework and the app is hosted on Azure App Service on Linux.
@@ -15,14 +15,14 @@ In this tutorial, you'll deploy a data-driven Python web app (**[Django](https:/
15
15
16
16
**To complete this tutorial, you'll need:**
17
17
18
-
* An Azure account with an active subscription exists. If you don't have an Azure account, you [can create one for free](https://azure.microsoft.com/free/python).
18
+
* An Azure account with an active subscription. If you don't have an Azure account, you [can create one for free](https://azure.microsoft.com/free/python).
19
19
* Knowledge of Python with Flask development or [Python with Django development](/training/paths/django-create-data-driven-websites/)
20
20
21
21
## Sample application
22
22
23
23
Sample Python applications using the Flask and Django framework are provided to help you follow along with this tutorial. To deploy them without running them locally, skip this part.
24
24
25
-
To run the application locally, make sure you have [Python 3.7 or higher](https://www.python.org/downloads/) and [PostgreSQL](https://www.postgresql.org/download/)install locally. Then, download or clone the app:
25
+
To run the application locally, make sure you have [Python 3.7 or higher](https://www.python.org/downloads/) and [PostgreSQL](https://www.postgresql.org/download/)installed locally. Then, download or clone the app:
26
26
27
27
### [Flask](#tab/flask)
28
28
@@ -124,9 +124,9 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
124
124
1. *Resource Group*→ Select **Create new** and use a name of **msdocs-python-postgres-tutorial**.
125
125
1. *Region*→ Any Azure region near you.
126
126
1. *Name*→**msdocs-python-postgres-XYZ** where *XYZ* is any three random characters. This name must be unique across Azure.
127
-
1. *Runtime stack*→**Python 3.9**.
127
+
1. *Runtime stack*→**Python 3.10**.
128
+
1. *Database*→**PostgreSQL - Flexible Server** is selected by default as the database engine. The server name and database name is also set by default to appropriate values.
128
129
1. *Hosting plan*→**Basic**. When you're ready, you can [scale up](manage-scale-up.md) to a production pricing tier later.
129
-
1. **PostgreSQL - Flexible Server** is selected by default as the database engine.
130
130
1. Select **Review + create**.
131
131
1. After validation completes, select **Create**.
132
132
:::column-end:::
@@ -149,8 +149,6 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
149
149
:::column-end:::
150
150
:::row-end:::
151
151
152
-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
153
-
154
152
## 2. Verify connection settings
155
153
156
154
The creation wizard generated the connectivity variables for you already as [app settings](configure-common.md#configure-app-settings).
@@ -165,15 +163,18 @@ The creation wizard generated the connectivity variables for you already as [app
165
163
:::row-end:::
166
164
:::row:::
167
165
:::column span="2":::
168
-
**Step 2.** In the **Application settings** tab of the **Configuration** page, verify that `DBNAME`, `DBHOST`, `DBUSER`, and `DBPASS` are present. They'll be injected into the runtime environment as environment variables.
169
-
App settings are a good way to keep connection secrets out of your code repository.
166
+
**Step 2.** In the **Application settings** tab of the **Configuration** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` is present. That will be injected into the runtime environment as an environment variable.
167
+
App settings are one way to keep connection secrets out of your code repository.
168
+
When you're ready to move your secrets to a more secure location,
169
+
here's an [article on storing in Azure Key Vault](/azure/key-vault/certificates/quick-create-python).
170
170
:::column-end:::
171
171
:::column:::
172
172
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png" alt-text="A screenshot showing how to see the autogenerated connection string." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png":::
173
173
:::column-end:::
174
174
:::row-end:::
175
175
176
-
Having issues? Refer first to the [Troubleshooting guide](configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/DjangoCLITutorialHelp).
176
+
Having issues? Check the [Troubleshooting guide](configure-language-python.md#troubleshooting).
177
+
177
178
178
179
## 3. Deploy sample code
179
180
@@ -226,6 +227,7 @@ In this step, you'll configure GitHub deployment using GitHub Actions. It's just
226
227
1. In **Organization**, select your account.
227
228
1. In **Repository**, select **msdocs-flask-postgresql-sample-app**.
228
229
1. In **Branch**, select **main**.
230
+
1. Keep the default option selected to **Add a workflow**.
229
231
1. In the top menu, select **Save**. App Service commits a workflow file into the chosen GitHub repository, in the `.github/workflows` directory.
230
232
:::column-end:::
231
233
:::column:::
@@ -325,13 +327,14 @@ In this step, you'll configure GitHub deployment using GitHub Actions. It's just
325
327
326
328
-----
327
329
328
-
Having issues? Refer first to the [Troubleshooting guide](configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/DjangoCLITutorialHelp).
330
+
Having issues? Check the [Troubleshooting guide](configure-language-python.md#troubleshooting).
331
+
329
332
330
333
## 4. Generate database schema
331
334
332
335
### [Flask](#tab/flask)
333
336
334
-
With the PostgreSQL database protected by the virtual network, the easiest way to run Run [Flask database migrations](https://flask-migrate.readthedocs.io/en/latest/) is in an SSH session with the App Service container.
337
+
With the PostgreSQL database protected by the virtual network, the easiest way to run [Flask database migrations](https://flask-migrate.readthedocs.io/en/latest/) is in an SSH session with the App Service container.
335
338
336
339
:::row:::
337
340
:::column span="2":::
@@ -380,8 +383,6 @@ With the PostgreSQL database protected by the virtual network, the easiest way t
380
383
381
384
-----
382
385
383
-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
384
-
385
386
## 5. Browse to the app
386
387
387
388
:::row:::
@@ -396,27 +397,25 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
396
397
:::row-end:::
397
398
:::row:::
398
399
:::column span="2":::
399
-
**Step 2.** Add a few tasks to the list.
400
-
Congratulations, you're running a secure data-driven Flask app in Azure App Service, with connectivity to Azure Database for PostgreSQL.
400
+
**Step 2.** Add a few restaurants to the list.
401
+
Congratulations, you're running a web app in Azure App Service, with secure connectivity to Azure Database for PostgreSQL.
401
402
:::column-end:::
402
403
:::column:::
403
404
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-browse-app-2.png" alt-text="A screenshot of the Flask web app with PostgreSQL running in Azure showing restaurants and restaurant reviews." lightbox="./media/tutorial-python-postgresql-app/azure-portal-browse-app-2.png":::
404
405
:::column-end:::
405
406
:::row-end:::
406
407
407
-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
408
-
409
408
## 6. Stream diagnostic logs
410
409
411
410
Azure App Service captures all messages output to the console to help you diagnose issues with your application. The sample app includes `print()` statements to demonstrate this capability as shown below.
@@ -425,6 +424,7 @@ Azure App Service captures all messages output to the console to help you diagno
425
424
**Step 1.** In the App Service page:
426
425
1. From the left menu, select **App Service logs**.
427
426
1. Under **Application logging**, select **File System**.
427
+
1. In the top menu, select **Save**.
428
428
:::column-end:::
429
429
:::column:::
430
430
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-stream-diagnostic-logs-1.png" alt-text="A screenshot showing how to enable native logs in App Service in the Azure portal." lightbox="./media/tutorial-python-postgresql-app/azure-portal-stream-diagnostic-logs-1.png":::
@@ -439,7 +439,7 @@ Azure App Service captures all messages output to the console to help you diagno
439
439
:::column-end:::
440
440
:::row-end:::
441
441
442
-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
442
+
Learn more about logging in Python apps in the series on [setting up Azure Monitor for your Python application](/azure/azure-monitor/app/opencensus-python]).
443
443
444
444
## 7. Clean up resources
445
445
@@ -474,8 +474,6 @@ When you're finished, you can delete all of the resources from your Azure subscr
474
474
:::column-end:::
475
475
:::row-end:::
476
476
477
-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
478
-
479
477
## Frequently asked questions
480
478
481
479
-[How much does this setup cost?](#how-much-does-this-setup-cost)
@@ -487,10 +485,10 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
487
485
488
486
#### How much does this setup cost?
489
487
490
-
Pricing for the create resources is as follows:
488
+
Pricing for the created resources is as follows:
491
489
492
490
- The App Service plan is created in **Basic** tier and can be scaled up or down. See [App Service pricing](https://azure.microsoft.com/pricing/details/app-service/linux/).
493
-
- The PostgreSQL flexible server is create in the lowest burstable tier **Standard_B1ms**, with the minimum storage size, which can be scaled up or down. See [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/).
491
+
- The PostgreSQL flexible server is created in the lowest burstable tier **Standard_B1ms**, with the minimum storage size, which can be scaled up or down. See [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/).
494
492
- The virtual network doesn't incur a charge unless you configure extra functionality, such as peering. See [Azure Virtual Network pricing](https://azure.microsoft.com/pricing/details/virtual-network/).
495
493
- The private DNS zone incurs a small charge. See [Azure DNS pricing](https://azure.microsoft.com/pricing/details/dns/).
496
494
@@ -502,7 +500,7 @@ Pricing for the create resources is as follows:
502
500
503
501
#### How does local app development work with GitHub Actions?
504
502
505
-
Take the autogenerated workflow file from App Service as an example, each `git push` kicks off a new build and deployment run. From a local clone of the GitHub repository, you make the desired updates push it to GitHub. For example:
503
+
Using the autogenerated workflow file from App Service as an example, each `git push` kicks off a new build and deployment run. From a local clone of the GitHub repository, you make the desired updates and push to GitHub. For example:
506
504
507
505
```terminal
508
506
git add .
@@ -533,11 +531,11 @@ For more information, see [Production settings for Django apps](configure-langua
533
531
534
532
#### I can't connect to the SSH session
535
533
536
-
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'DBNAME'`, it may mean that the environment variable is missing (you may have removed the app setting).
534
+
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'AZURE_POSTGRESQL_CONNECTIONSTRING'`, it may mean that the environment variable is missing (you may have removed the app setting).
537
535
538
536
#### I get an error when running database migrations
539
537
540
-
If you encounter any errors related to connecting to the database, check if the app settings (`DBHOST`, `DBNAME`, `DBUSER`, and `DBPASS`) have been changed. Without those settings, the migrate command can't communicate with the database.
538
+
If you encounter any errors related to connecting to the database, check if the app settings (`AZURE_POSTGRESQL_CONNECTIONSTRING`) have been changed. Without that connection string, the migrate command can't communicate with the database.
0 commit comments