Skip to main content

Store application data in Azure PostgreSQL

Connect a Builder app to Azure Database for PostgreSQL Flexible Server using managed identity instead of a stored password. A Node.js sample with a React frontend keeps SQL queries and Entra tokens on the server.

Preview sample: development and testing only

Use synthetic data and a dedicated test database; don't relax an existing server's firewall. Managed identity authenticates the app to PostgreSQL, not visitors. Writes default off; enabling them lets every visitor change shared demo data, so require visitor sign-in restricted to testers first.

Prerequisites​

  • An Azure subscription in West US 2 (westus2) with Node.js 24 support.
  • Git, Node.js 24/npm, Azure CLI with Bicep, and permission to provision PostgreSQL and set its Microsoft Entra administrator.
  • A GitHub fork with the Builder GitHub App installed before provisioning, including public forks (GitHub authorization).
  • Approved PostgreSQL network paths from your workstation and, separately, from Builder, which has no VNet/private endpoint connectivity or stable outbound IP.

1. Prepare the sample and database​

Clone the sample​

Fork the PostgreSQL sample and clone your fork:

git clone "<your-fork-https-url>" shopstream
cd shopstream

Provision PostgreSQL​

Set your IDs, a new test group, a globally unique server name, and your bootstrap administrator:

export AZURE_SUBSCRIPTION_ID="<subscription-id>"
export AZURE_TENANT_ID="<tenant-id>"
export RG="<new-test-resource-group>"
export PG_SERVER="<globally-unique-postgresql-server-name>"
export PG_LOCATION="westus2"
export ADMIN_OBJECT_ID="<bootstrap-admin-object-id>"
export ADMIN_NAME="<bootstrap-admin-upn-or-group-name>"

az login --tenant "$AZURE_TENANT_ID"
az group create --subscription "$AZURE_SUBSCRIPTION_ID" \
--name "$RG" --location "$PG_LOCATION"
az deployment group what-if --subscription "$AZURE_SUBSCRIPTION_ID" \
--resource-group "$RG" --name asb-pg-dependency \
--template-file infra/main.bicep \
--parameters serverName="$PG_SERVER" location="$PG_LOCATION" \
administratorObjectId="$ADMIN_OBJECT_ID" administratorName="$ADMIN_NAME"

Review database costs. The template creates PostgreSQL 16 Burstable Standard_B1ms with 32 GiB storage, seven-day backups, no high availability, and Entra-only authentication (no passwords or firewall rules). For a group administrator, add administratorType=Group to both deployment commands; never make the runtime app the administrator.

az deployment group create --subscription "$AZURE_SUBSCRIPTION_ID" \
--resource-group "$RG" --name asb-pg-dependency \
--template-file infra/main.bicep \
--parameters serverName="$PG_SERVER" location="$PG_LOCATION" \
administratorObjectId="$ADMIN_OBJECT_ID" administratorName="$ADMIN_NAME"
az deployment group show --subscription "$AZURE_SUBSCRIPTION_ID" \
--resource-group "$RG" --name asb-pg-dependency \
--query properties.outputs -o json

Record serverId, host, and databaseName.

Configure network access​

Follow the sample README to permit only your workstation's exact IPv4 for bootstrap; that rule doesn't grant Builder connectivity.

Development/test Azure-services exception

Builder needs Allow Azure services on the dedicated demo server, which permits Azure-origin workloads across subscriptions and tenants; it is not app-scoped and not an all-internet rule. The template doesn't enable it. Enable it only if your policy permits this scope, and keep Entra-only authentication, verified TLS, and nonadministrator runtime grants.

az postgres flexible-server firewall-rule create \
--server-name "$PG_SERVER" --resource-group "$RG" \
--name devtest-allow-azure-services --start-ip-address 0.0.0.0 \
--end-ip-address 0.0.0.0 --subscription "$AZURE_SUBSCRIPTION_ID"

Initialize the database​

In your provisioning terminal, bootstrap as the administrator:

npm ci
export PGHOST="$PG_SERVER.postgres.database.azure.com"
export PGDATABASE=shopstream
export PGUSER="$ADMIN_NAME"
export ENTRA_CREDENTIAL=azure-cli
unset DATABASE_URL PGPASSWORD PGSSLMODE
npm run db:bootstrap

This is the populated sample's required first-run setup: it installs the schema and 2,525 fictional rows, including 10 vendors, 15 categories, 500 products and seeded customers/orders/reviews. These are real persisted demonstration records, not a fake connected dashboard. Connections and resource creation do not create schema or data.

Bootstrap requires an empty dedicated database and uses one transaction, an advisory lock and a version marker. Rerunning version 1 leaves data unchanged; existing unrelated tables or an empty/unknown marker are rejected. Never run db/seed.sql directly. Vendor contacts use reserved .invalid domains. Setup runs as the authorized administrator, not the runtime identity, and never during build/startup. Use a separate schema-owner or migration principal for later changes and review future grants.

2. Create the app and enable managed identity​

Keep the root manifest's settings and the separate client/ package root. Use your test group and West US 2. In Authentication, require Microsoft Entra sign-in. After creation, enable a system-assigned identity on the app's Identity page. If initial health fails before database configuration, keep the app and finish the grants below.

Keep the fork, branch, and root builder.yaml you prepared above. Builder deploys committed GitHub source, not uncommitted local files.

Open the App Service Builder Portal. Your Azure account needs permission to create and deploy Microsoft.Web/builderApps resources. Creating a resource group also requires permission to create groups in the selected subscription.

  1. Sign in to the Portal with your Microsoft account.

  2. Select the subscription and choose Create.

  3. In Choose approach, keep Let Builder handle it selected and select Next.

    Create Builder App wizard with Let Builder handle it selected beside ZIP deploy; the steps include Connect GitHub, Authentication, App details, and Review.
    Choose Builder-managed GitHub source for this procedure. ZIP deploy is the separate local-source path; include a root builder.yaml for ZIP deployments. Review authentication and the target before submitting either approach.
  4. In Connect GitHub, sign in to GitHub and authorize the OAuth App. Keep Use my repository selected to deploy the fork you prepared above. Choose the account or organization that owns your fork, then select that repository and branch. Use Change account if you need a different GitHub account, or Refresh access after changing repository permissions. The App Service Builder GitHub App needs access to this repository to read your code, build it, and deploy your app. Signing in to GitHub alone does not grant this access. If prompted, select Install GitHub App and grant access to your selected repository, or add it to an existing installation. Close the GitHub window to refresh access, or select Refresh access after approval. Select Next when Builder is connected and GitHub App access confirmed are shown. To keep the defaults, select Review directly once it is enabled. Builder generates an app name, uses the selected subscription and first available resource group, and supplies the app region. You do not need to visit Authentication or App details when their defaults meet your needs. Review the authentication summary before creating; organization-required sign-in defaults still apply.

  5. In Authentication, configure visitor sign-in. Review the sign-in mode; some organization directories default to required tenant sign-in. When offered, select Apply under Require sign-in with a work account from your organization to limit the app to accounts in your Microsoft Entra directory. Select Off only if you intend to allow anonymous visitors. Select Next.

  6. In App details, enter an app name and select the subscription, resource group, and West US 2 (westus2). To create a resource group, select Create new, enter its name, and choose Resource group region. Select Add. The group appears as (new) and is created when you submit. Its region stores the resource group's metadata and does not change the app's region. Select Next.

  7. On Review and create, confirm the app, repository, branch, subscription, resource group, app region, and authentication choices. Select Create and deploy.

  8. Open the app's Versions page and wait for the deployment to succeed. Open a version to inspect its source revision, component results, and build logs. Choose View logs to inspect the latest deployment's steps and logs. On Overview, use Browse to open the app URL.

Managed identity is separate from visitor sign-in and is not assigned during Portal creation. To let the app access Azure resources, configure managed identity afterward from the app's Identity page.

Already prepared a fork? Keep Use my repository selected so you deploy that source, not a second copy. To start with a new sample instead, follow Create from a Portal template. Use GitHub Actions is marked Coming soon; use the Builder-managed approach for this preview.

If group creation fails, app creation does not start. If the group was created before a later failure, it remains in Azure; cancelling does not delete it. Check the existing group and app before retrying, and inspect a group's contents before deleting it separately.

See GitHub authorization requirements for organization approvals and SSO. If app creation succeeds but deployment cannot start, read the error and retry deployment on the existing app rather than creating a duplicate. For “Source revision does not contain builder.yaml”, push the missing manifest before retrying.

For API automation, use the ARM API deployment guide with this same fork, branch, and source root ".". ARM and Bicep infrastructure templates provision the resource; separately authorize and connect this source, then deploy it through CLI or ARM.

  1. On Authentication, require Microsoft Entra sign-in and Limit to specific identities with your tenant and exact tester object IDs (a tenant-only rule permits everyone in the directory).
  2. On Identity, enable the system-assigned identity and select Save and deploy, then record its principal ID. Usable app health still requires the grants and variables below.

3. Grant database access and configure the app​

In the same terminal, grant access to the app principal:

export RUNTIME_ROLE=shopstream_runtime
export RUNTIME_OBJECT_ID="<app-identity-principal-id>"
export RUNTIME_OBJECT_TYPE=service
npm run db:grant

The script maps the app's principal/object ID (not client ID) and grants bounded table, column, and sequence access in shopstream, with no administrator membership or schema ownership. See Entra role management.

On Connections, add PostgreSQL, choose the provisioned flexible server and already assigned app system identity. Save these mapped runtime settings:

Builder variableValue
PGHOSTThe host output
PGDATABASEshopstream
PGUSERshopstream_runtime, never the administrator

Keep ENTRA_CREDENTIAL=managed-identity in Variables (or omit its default), with AZURE_CLIENT_ID unset for the system identity. Keep SHOPSTREAM_ENABLE_DEMO_WRITES unset/false initially and set SHOPSTREAM_PUBLIC_ORIGIN to the exact runtime HTTPS origin only for approved writes. These are app controls, not connection fields.

Keep passwords, tokens, and PORT out of Builder variables. Set the write origin without a trailing slash, path, query, fragment, credentials, or wildcard. The sample does not trust forwarded host/protocol headers. The server gets a fresh Entra token for each new database connection.

Named connection mappings​

The managed-identity named Connections workflow completed hosted Production qualification for PostgreSQL with both system-assigned and user-assigned identities. Select the already-assigned identity and your existing PostgreSQL server. Map host to PGHOST, database to PGDATABASE, and user to PGUSER. Set the database to shopstream and user to the granted shopstream_runtime role, not the administrator. This sample requires database and user explicitly; optional flags in the general connection profile do not override its application contract. Keep PGPORT=5432 or let the sample use its own port default. Remove the optional PGSSLMODE row and password row: the existing sample rejects both and owns verified TLS and token acquisition in code. Keep ENTRA_CREDENTIAL, visitor-access/write controls and any documented UAMI selection in ordinary app variables. Do not declare the same exact variable name in both manual variables and connection settings. Follow the save-without-deploying migration sequence when replacing manual entries. Saving mappings neither grants database access nor deploys; use a new deployment and the checks below. This is not a password-authentication example or evidence of hosted Connections qualification. The platform's Password alternative is a different client contract; this sample remains Entra-only. Use the Builder CLI procedure with a release that includes app connections, or Azure CLI/ARM. Builder does not grant PostgreSQL data access; the database administrator creates and grants the role.

4. Deploy and verify the connection​

Saved settings don't affect the running version until deployment.

  1. Build and deploy a new version and wait for success.
  2. Open the URL from Overview.
  3. Confirm the dashboard, products, and orders are populated, Read-only demo is shown, and Place Test Order is disabled.

/health queries PostgreSQL.

Actual connected marketplace dashboard with 300 synthetic orders, 200 customers and 500 products.

The figures and customer names are fictional seeded data. A repeat bootstrap preserves the installed dataset; updating sample source does not silently replace existing rows.

Optional: Evaluate test writes​

After requiring tester-only sign-in, set the exact SHOPSTREAM_PUBLIC_ORIGIN and SHOPSTREAM_ENABLE_DEMO_WRITES=true and deploy a new version.

  1. On Orders, select Place Test Order and record its ID.
  2. Select Mark processing, refresh, and confirm the same order remains processing.
  3. Create one synthetic product review and delete only that review.
  4. Deploy a new version and confirm the recorded order still exists with the same status.

Sample Orders page showing a synthetic test order in Processing.

Illustrative write-enabled state from an earlier synthetic-data run, not the initial read-only dashboard. Enabling writes and verifying one test order are separate steps.

Turn demo writes off and deploy again when finished.

Troubleshoot connection failures​

Check principal mapping, grants, variables, and network path with service connection diagnostics. Don't disable TLS or use administrator credentials to hide an error.

Clean up resources​

To restore the dataset, stop the app and follow the README's administrator-only db:reset procedure (it replaces all eight tables' data).

Delete only the test app you created when you no longer need it. App deletion does not remove your GitHub repository, resource group, or separately owned services and user-assigned identities.

On updated targets, deletion first reports that it was accepted and then continues in the background. In the Portal, keep the Deleting app's progress available until it completes. With the updated Builder CLI, deletion waits by default; --no-wait returns an operation URL that you can inspect with builder app status. Closing the client does not cancel an accepted deletion. Recently published apps can wait for the remaining one-hour upload grant plus a two-minute drain, and provider failures can extend the wait. The same app name in the same subscription and resource group stays unavailable until deletion finishes. Full artifact cleanup remains under construction; existing lifecycles retain their earlier cleanup scope.

Return to the app inventory and choose Delete from the test app's actions. Alternatively, open the app's Settings and select Delete under Danger zone. Enter the exact app name and choose Delete. The dialog closes when the service accepts the request, without waiting for cleanup to finish. The app remains visible as Deleting, with changes disabled while cleanup runs. Deleting from Settings keeps the app open so you can follow its deletion progress.

A notification names the app and reports progress, then success or failure. You can open another app without losing deletion progress. Use the bell beside Docs and All activity to review it. If the request is rejected, the dialog shows the error and lets you retry. Deletion progress includes the current cleanup step and retry details when available. Wait for deletion to complete before creating another app with the same name. Refreshing or closing the Portal clears this session's tracking; it does not cancel server-side cleanup.

Deleting the Builder app doesn't delete PostgreSQL, backups, or the database-principal mapping. If keeping the server, follow the README to remove the app principal and the firewall rules you created. To delete it, remove those rules first, confirm ownership, and accept permanent data loss (never delete a shared resource group):

az postgres flexible-server delete --subscription "$AZURE_SUBSCRIPTION_ID" \
--resource-group "$RG" --name "$PG_SERVER"