Exercise - Connect to a database

Completed

Because you deployed the Spring PetClinic application with the default H2 in-memory database, you might want to connect it to a more powerful, persistent database like PostgreSQL. Using a persistent database ensures that your application data is stored even when the application is restarted, providing greater durability and scalability for production use.

Note

This unit is optional. You can skip it if you're already familiar with database configurations.

Create the PostgreSQL server

Use the following commands to set environment variables used to create the database connections:

export POSTGRESQLSERVER="petclinic-server"
export DATABASE="petclinic"

Update spring.profiles.active for PostgreSQL

Use the following command to update the environment variables of the deployed container apps to set the database as PostgreSQL:

az containerapp update \
    --resource-group $RESOURCE_GROUP \
    --name $APP_NAME \
    --set-env-vars "spring.profiles.active"="postgres"

Create a new database

To create a database within a new PostgreSQL flexible server instance, use the following steps:

  1. Create a PostgreSQL flexible server by using the following command:

    az postgres flexible-server create \
        --resource-group $RESOURCE_GROUP \
        --name $POSTGRESQLSERVER
    
  2. Find the auto-generated admin username and password in the output, then save these credentials in a secure place. You can optionally use them later to connect and configure the database.

  3. Create a new database in the PostgreSQL flexible server instance by using the following command:

    az postgres flexible-server db create \
        --resource-group $RESOURCE_GROUP \
        --database-name $DATABASE \
        --server-name $POSTGRESQLSERVER
    

    For more information, see Quickstart: Create an instance of Azure Database for PostgreSQL - Flexible Server.

Connect via Service Connector

Next, connect your previously deployed container app to the PostgreSQL database by using the following steps:

  1. In the Azure portal, navigate to your Azure Container Apps instance.

  2. Go to Settings > Service Connector.

  3. Select Create, and then fill out the connection form using the following details:

    • Basic:
      • For Service type, use DB for PostgreSQL flexible server.
      • For PostgreSQL flexible server, use petclinic-server.
      • For PostgreSQL database, use petclinic.
      • For Client type, use SpringBoot.
    • Authentication:
      • Select Connection string.
      • Fill in values for the Username and Password fields, and leave the other fields with their default settings.

    Screenshot of the Azure portal that shows the Service Connector (preview) page and the Create connection pane.

  4. Confirm and deploy your changes.

  5. After the deployment is ready, select Validate to validate your connection status.

For more information, see Connect to PostgreSQL Database from a Java Quarkus Container App without secrets using a managed identity.

Validate the database setup

The configuration is complete. If you connect to your PostgreSQL database, you can see that the table was created, which should be similar to the one shown in the following example:

           List of relations
| Schema | Name            | Type  |
|--------|-----------------|-------|
| public | owners          | table |
| public | pets            | table |
| public | specialties     | table |
| public | types           | table |
| public | vet_specialties | table |
| public | vets            | table |
| public | visits          | table |