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
- Install and run [PostgreSQL](https://www.postgresql.org/download/).
41
41
42
-
## Connect to your local PostgreSQL server and create a database
42
+
## Create a local PostgreSQL database
43
43
44
-
First, create and connect to a local PostgreSQL server and database:
44
+
First, connect to your local PostgreSQL server and create a database:
45
45
46
46
In a local terminal window, run `psql` to connect to your local PostgreSQL server as the built-in `postgres` user.
47
47
@@ -55,7 +55,7 @@ If the connection isn't successful, make sure your PostgreSQL server is running,
55
55
pg_ctl.exe restart -D "<postgresql data directory>"
56
56
```
57
57
58
-
In your *postgres* directory, create a new database called *pollsdb*, and set up a new database user named *manager* with password *supersecretpass*:
58
+
Create a new database called *pollsdb*, and set up a database user named *manager* with password *supersecretpass*:
59
59
60
60
```sql
61
61
CREATEDATABASEpollsdb;
@@ -71,11 +71,11 @@ Type `\q` to exit the PostgreSQL client.
71
71
72
72
Next, set up and run the sample Python Django web app.
73
73
74
-
The [djangoapp](https://github.com/Azure-Samples/djangoapp) sample repository contains the finished data-driven [Django](https://www.djangoproject.com/) polls app you get from following [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this, or any other data-driven Python Django web app, to a local or cloud PostgreSQL database back end, and deploy the app to Azure App Service.
74
+
The [djangoapp](https://github.com/Azure-Samples/djangoapp) sample repository contains the data-driven [Django](https://www.djangoproject.com/) polls app you get by following [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this, or any other data-driven Python Django web app, to a local or cloud PostgreSQL database back end.
75
75
76
76
### Clone the sample app
77
77
78
-
In a terminal window, run the following commands to clone the sample app repository and change to the new working directory:
78
+
In a terminal window, run the following commands to clone the sample app repository, and change to the new working directory:
In the `venv` environment, change to your *djangoapp* folder and run *env.sh* or *env.ps1* to set the environment variables *azuresite/settings.py* will use for the database connection settings.
99
+
In the `venv` environment, run *env.sh* or *env.ps1* to set the environment variables *azuresite/settings.py* will use for the database connection settings.
100
100
101
101
```bash
102
102
source ./env.sh
@@ -148,9 +148,9 @@ To stop the Django server, type Ctrl+C in the terminal.
148
148
149
149
Most of the remaining steps in this article use Azure CLI commands in the Azure Cloud Shell.
150
150
151
-
## Create and connect to an Azure Database for PostgreSQL database
151
+
## Create and connect to Azure Database for PostgreSQL
152
152
153
-
In this section, you create an Azure Database for PostgreSQL database, and connect your web app to it. When you deploy your web app to Azure App Service, the app uses this cloud database.
153
+
In this section, you create an Azure Database for PostgreSQL server and database, and connect your web app to it. When you deploy your web app to Azure App Service, the app uses this cloud database.
154
154
155
155
### Create a resource group
156
156
@@ -160,20 +160,23 @@ You can create a new resource group for your Azure Database for PostgreSQL serve
160
160
161
161
### Create an Azure Database for PostgreSQL server
162
162
163
-
You create a PostgreSQL server with the [`az postgres server create`](/cli/azure/postgres/server?view=azure-cli-latest#az-postgres-server-create) command in the Cloud Shell.
163
+
You create a PostgreSQL server with the [az postgres server create](/cli/azure/postgres/server?view=azure-cli-latest#az-postgres-server-create) command in the Cloud Shell.
164
164
165
-
In the following command, replace *\<postgresql-name>* with a unique server name. The server name is part of your PostgreSQL endpoint *https://<postgresql-name>.postgres.database.azure.com*, so the name needs to be unique across all servers in Azure.
165
+
> [!NOTE]
166
+
> Before you create an Azure Database for PostgreSQL server, check which [compute generation](/azure/postgresql/concepts-pricing-tiers#compute-generations-and-vcores) is available in your region. If your region doesn't support Gen4 hardware, change *--sku-name* in the following command line to a value that's supported in your region, such as Gen5.
167
+
168
+
In the following command, replace *\<postgresql-name>* with a unique server name. The server name is part of your PostgreSQL endpoint *https://\<postgresql-name>.postgres.database.azure.com*, so the name needs to be unique across all servers in Azure.
166
169
167
-
Replace *\<resourcegroup-name>* and *\<region>* with the name and region of the resource group you created or want to use. Replace *\<admin-username>* and *\<admin-password>* with user credentials for the database administrator account.
170
+
Replace *\<resourcegroup-name>* and *\<region>* with the name and region of the resource group you want to use. Replace *\<admin-username>* and *\<admin-password>* with user credentials for the database administrator account.
168
171
169
172
> [!NOTE]
170
-
> Before you create an Azure Database for PostgreSQL server, check which [compute generation](https://docs.microsoft.com/azure/postgresql/concepts-pricing-tiers#compute-generations-and-vcores) is available in your region. If your region doesn't support Gen4 hardware, change *--sku-name* in the following command line to a value that's supported in your region, such as Gen5.
173
+
> Remember the *\<admin-username>*and*\<admin-password>*to use later for signing in to the PostgreSQL server and databases.
171
174
172
175
```azurecli-interactive
173
176
az postgres server create --resource-group <resourcegroup-name> --name <postgresql-name> --location "<region>" --admin-user <admin-username> --admin-password <admin-password> --sku-name B_Gen4_1
174
177
```
175
178
176
-
When the Azure Database for PostgreSQL server is created, the Azure CLI shows information similar to the following example:
179
+
After the Azure Database for PostgreSQL server is created, the Azure CLI returns JSON code like the following example:
177
180
178
181
```json
179
182
{
@@ -194,29 +197,26 @@ When the Azure Database for PostgreSQL server is created, the Azure CLI shows in
194
197
}
195
198
```
196
199
197
-
> [!NOTE]
198
-
> Remember the *\<admin-username>* and *\<admin-password>* to use later for signing in to the PostgreSQL server and databases.
199
-
200
200
### Create firewall rules for the Azure Database for PostgreSQL server
201
201
202
-
In the Cloud Shell, run the following Azure CLI command to allow access to the database from Azure resources. Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
202
+
Run the [az postgres server firewall-rule create](/cli/azure/postgres/server/firewall-rule#az-postgres-server-firewall-rule-create) command to allow access to the database from Azure resources. Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
203
203
204
204
```azurecli-interactive
205
205
az postgres server firewall-rule create --resource-group <resourcegroup-name> --server-name <postgresql-name> --start-ip-address=0.0.0.0 --end-ip-address=0.0.0.0 --name AllowAllAzureIPs
206
206
```
207
207
208
208
> [!NOTE]
209
-
> The preceding setting allows network connections from all IP addresses within the Azure network. For production use, try to configure the most restrictive firewall rules possible by [using only the outbound IP addresses your app uses](../overview-inbound-outbound-ips.md?toc=%2fazure%2fapp-service%2fcontainers%2ftoc.json#find-outbound-ips).
209
+
> The preceding setting allows network connections from all IP addresses within the Azure network. For production use, try to configure the most restrictive firewall rules possible by [allowing only the outbound IP addresses your app uses](../overview-inbound-outbound-ips.md?toc=%2fazure%2fapp-service%2fcontainers%2ftoc.json#find-outbound-ips).
210
210
211
-
Run the `firewall-rule-create` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
211
+
Run the `firewall-rulecreate` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your own values.
212
212
213
213
```azurecli-interactive
214
214
az postgres server firewall-rule create --resource-group <resourcegroup-name> --server-name <postgresql-name> --start-ip-address=<your-ip-address> --end-ip-address=<your-ip-address> --name AllowLocalClient
215
215
```
216
216
217
217
### Create and connect to the Azure Database for PostgreSQL database
218
218
219
-
In the Cloud Shell, connect to the Azure Database for PostgreSQL server by running the following command. Use the server name, admin username, and password you created for the server.
219
+
Connect to your Azure Database for PostgreSQL server by running the following command. Use your own *\<postgresql-name>* and *\<admin-username>*, and sign in with the password you created for the server.
@@ -230,14 +230,13 @@ CREATE USER manager WITH PASSWORD 'supersecretpass';
230
230
GRANT ALL PRIVILEGES ON DATABASE pollsdb TO manager;
231
231
```
232
232
233
-
Type `\q` to exit the PostgreSQL client.
234
-
235
233
> [!NOTE]
236
-
> It's a best practice to create database users with restricted permissions for specific apps, instead of using the admin user. In this example, the `manager` user has full privileges to *only* the `pollsdb` database.
234
+
> It's a best practice to create database users with restricted permissions for specific apps, instead of using the admin user. The `manager` user has full privileges to *only* the `pollsdb` database.
235
+
Type `\q` to exit the PostgreSQL client.
237
236
238
237
### Test app connectivity to the Azure Database for PostgreSQL production database
239
238
240
-
Edit your local *env.sh* or *env.ps1* file to change the environment variables to your cloud PostgreSQL database by replacing *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
239
+
Edit your local *env.sh* or *env.ps1* file to point to to your cloud PostgreSQL database, by replacing *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
Run Django migration to the Azure database and create an admin user.
264
+
Run Django migration to the Azure database, and create an admin user.
266
265
267
266
```bash
268
267
python manage.py migrate
@@ -275,13 +274,13 @@ Once the admin user is created, run the Django server.
275
274
python manage.py runserver
276
275
```
277
276
278
-
In a browser, go to *http:\//localhost:8000* again. You should see the message **No polls are available** again.
277
+
In a browser, go to *http:\//localhost:8000*, and you should see the message **No polls are available** again.
279
278
280
-
Go to *http:\//localhost:8000/admin* and sign in using the admin user you created, and create a poll question like before.
279
+
Go to *http:\//localhost:8000/admin*, sign in using the admin user you created, and create a poll question like before.
281
280
282
281

283
282
284
-
Go to *http:\//localhost:8000* again and see the poll question displayed. Your app is now writing data to the Azure Database for PostgreSQL database.
283
+
Go to *http:\//localhost:8000* again, and see the poll question displayed. Your app is now writing data to the Azure Database for PostgreSQL database.
285
284
286
285
## Deploy the web app to Azure App Service
287
286
@@ -293,15 +292,15 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
293
292
294
293
1. Django validates the `HTTP_HOST` header in incoming requests. For your Django web app to work in App Service, you need to add the fully qualified domain name of the app to the allowed hosts.
295
294
296
-
Edit *azuresite/settings.py* to change the `ALLOWED_HOSTS` line as follows.
295
+
Edit *azuresite/settings.py* to change the `ALLOWED_HOSTS` line as follows:
1. Django doesn't support [serving static files in production](https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/). For this tutorial, you use [WhiteNoise](https://whitenoise.evans.io/en/stable/) to enable serving static files in production. The WhiteNoise package was already installed with *requirements.txt*.
301
+
1. Django doesn't support [serving static files in production](https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/). For this tutorial, you use [WhiteNoise](https://whitenoise.evans.io/en/stable/) to enable serving the needed files. The WhiteNoise package was already installed with *requirements.txt*.
303
302
304
-
To configure Django to use WhiteNoise, in *azuresite/settings.py*, find the `MIDDLEWARE` setting, and add the `whitenoise.middleware.WhiteNoiseMiddleware`middleware to the list, just after the `django.middleware.security.SecurityMiddleware` middleware line. Your `MIDDLEWARE` setting should look like this:
303
+
To configure Django to use WhiteNoise, in *azuresite/settings.py*, find the `MIDDLEWARE` setting, and add `whitenoise.middleware.WhiteNoiseMiddleware` to the list, just after the `django.middleware.security.SecurityMiddleware` line. Your `MIDDLEWARE` setting should look like this:
305
304
306
305
```python
307
306
MIDDLEWARE= [
@@ -323,7 +322,7 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
323
322
> [!IMPORTANT]
324
323
> The database settings section already follows the security best practice of using environment variables. For complete deployment recommendations, see [Django Documentation: deployment checklist](https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/).
325
324
326
-
Commit your changes into your fork of the repository:
325
+
Commit your changes into your fork of the *djangoapp*repository:
Earlier in the tutorial, you defined environment variables to connect to your PostgreSQL database.
347
346
348
-
In App Service, you set environment variables as *app settings* by using the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command in Cloud Shell.
347
+
In Azure App Service, you set environment variables as *app settings* by using the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command.
349
348
350
-
The following example specifies the database connection details as app settings.
349
+
In the Azure Cloud Shell, run the following command to specify the database connection details as app settings. Replace *\<app-name>*, *\<resourcegroup-name>*, and *\<postgresql-name>* with your own values.
351
350
352
351
```azurecli-interactive
353
-
az webapp config appsettings set --name <app-name> --resource-group myResourceGroup --settings DBHOST="<postgresql-name>.postgres.database.azure.com" DBUSER="manager@<postgresql-name>" DBPASS="supersecretpass" DBNAME="pollsdb"
352
+
az webapp config appsettings set --name <app-name> --resource-group <resourcegroup-name> --settings DBHOST="<postgresql-name>.postgres.database.azure.com" DBUSER="manager@<postgresql-name>" DBPASS="supersecretpass" DBNAME="pollsdb"
354
353
```
355
354
356
-
For information on how these app settings are accessed in your code, see [Access environment variables](how-to-configure-python.md#access-environment-variables).
355
+
For information on how your code accesses these app settings, see [Access environment variables](how-to-configure-python.md#access-environment-variables).
357
356
358
357
### Push to Azure from Git
359
358
@@ -385,9 +384,9 @@ The App Service deployment server sees *requirements.txt* in the repository root
385
384
386
385
### Browse to the Azure app
387
386
388
-
Browse to the deployed app with URL *https:\//\<app-name>.azurewebsites.net*. It takes some time to start because the container needs to be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
387
+
Browse to the deployed app with URL *https:\//\<app-name>.azurewebsites.net*. It takes some time to start, because the container must be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
389
388
390
-
You should see the poll question that you created earlier.
389
+
You should see the poll questions that you created earlier.
391
390
392
391
App Service detects a Django project in your repository by looking for a *wsgi.py* file in each subdirectory, which `manage.py startproject` creates by default. When App Service finds the file, it loads the Django web app. For more information on how App Service loads Python apps, see [Configure built-in Python image](how-to-configure-python.md).
393
392
@@ -403,7 +402,7 @@ Go to *https:\//\<app-name>.azurewebsites.net/admin* and sign in using the admin
403
402
404
403
## Manage your app in the Azure portal
405
404
406
-
In the [Azure portal](https://portal.azure.com) search for and select the app you created.
405
+
In the [Azure portal](https://portal.azure.com), search for and select the app you created.
407
406
408
407

0 commit comments