Skip to content

Latest commit

 

History

History
138 lines (88 loc) · 7.67 KB

configure-spring-data-jpa-with-azure-postgresql.md

File metadata and controls

138 lines (88 loc) · 7.67 KB
title description ms.date ms.author author ms.topic ms.custom zone_pivot_group_filename zone_pivot_groups
Use Spring Data JPA with Azure Database for PostgreSQL
Learn how to use Spring Data JPA with an Azure Database for PostgreSQL database.
02/22/2023
bbenz
KarlErickson
article
devx-track-java, devx-track-azurecli, team=cloud_advocates, passwordless-java, spring-cloud-azure, devx-track-extended-java
java/java-zone-pivot-groups.json
passwordless-postgresql

Use Spring Data JPA with Azure Database for PostgreSQL

This tutorial demonstrates how to store data in Azure Database for PostgreSQL using Spring Data JPA.

The Java Persistence API (JPA) is the standard Java API for object-relational mapping.

In this tutorial, we include two authentication methods: Microsoft Entra authentication and PostgreSQL authentication. The Passwordless tab shows the Microsoft Entra authentication and the Password tab shows the PostgreSQL authentication.

Microsoft Entra authentication is a mechanism for connecting to Azure Database for PostgreSQL using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.

PostgreSQL authentication uses accounts stored in PostgreSQL. If you choose to use passwords as credentials for the accounts, these credentials will be stored in the user table. Because these passwords are stored in PostgreSQL, you need to manage the rotation of the passwords by yourself.

[!INCLUDE spring-data-prerequisites.md]

  • PostgreSQL command line client.

  • If you don't have a Spring Boot application, create a Maven project with the Spring Initializr. Be sure to select Maven Project and, under Dependencies, add the Spring Web, Spring Data JDBC, and PostgreSQL Driver dependencies, and then select Java version 8 or higher.

::: zone pivot="postgresql-passwordless-flexible-server"

Important

To use passwordless connections, configure the Microsoft Entra admin user for your Azure Database for PostgreSQL Flexible Server instance. For more information, see Manage Microsoft Entra roles in Azure Database for PostgreSQL - Flexible Server.

See the sample application

In this tutorial, you'll code a sample application. If you want to go faster, this application is already coded and available at https://github.com/Azure-Samples/quickstart-spring-data-jpa-postgresql.

[!INCLUDE spring-data-azure-postgresql-flexible-server-setup.md]

Configure Spring Boot to use Azure Database for PostgreSQL

To store data from Azure Database for PostgreSQL using Spring Data JPA, follow these steps to configure the application:

  1. Configure Azure Database for PostgreSQL credentials by adding the following properties to your application.properties configuration file.

    logging.level.org.hibernate.SQL=DEBUG
    
    spring.datasource.url=jdbc:postgresql://postgresqlflexibletest.postgres.database.azure.com:5432/demo?sslmode=require
    spring.datasource.username=<your_postgresql_ad_non_admin_username>
    spring.datasource.azure.passwordless-enabled=true
    
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    logging.level.org.hibernate.SQL=DEBUG
    
    spring.datasource.url=jdbc:postgresql://postgresqlflexibletest.postgres.database.azure.com:5432/demo?sslmode=require
    spring.datasource.username=<your_postgresql_non_admin_username>
    spring.datasource.password=<your_postgresql_non_admin_password>
    
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

::: zone-end

::: zone pivot="postgresql-passwordless-single-server"

Important

To use passwordless connections, configure the Microsoft Entra admin user for your Azure Database for PostgreSQL Single Server instance. For more information, see Use Microsoft Entra ID for authentication with PostgreSQL.

See the sample application

In this article, you'll code a sample application. If you want to go faster, this application is already coded and available at https://github.com/Azure-Samples/quickstart-spring-data-jpa-postgresql.

[!INCLUDE spring-data-azure-postgresql-single-server-setup.md]

Configure Spring Boot to use Azure Database for PostgreSQL

To store data from Azure Database for PostgreSQL using Spring Data JPA, follow these steps to configure the application:

  1. Configure Azure Database for PostgreSQL credentials by adding the following properties to your application.properties configuration file.

    logging.level.org.hibernate.SQL=DEBUG
    
    spring.datasource.url=jdbc:postgresql://postgresqlsingletest.postgres.database.azure.com:5432/demo?sslmode=require
    spring.datasource.username=<your_postgresql_ad_non_admin_username>@postgresqlsingletest
    spring.datasource.azure.passwordless-enabled=true
    
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    logging.level.org.hibernate.SQL=DEBUG
    
    spring.datasource.url=jdbc:postgresql://postgresqlsingletest.postgres.database.azure.com:5432/demo?sslmode=require
    spring.datasource.username=<your_postgresql_non_admin_username>@postgresqlsingletest
    spring.datasource.password=<your_postgresql_non_admin_password>
    
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

::: zone-end

[!INCLUDE spring-data-jpa-create-application.md]

[!INCLUDE deploy-to-azure-spring-apps]

Next steps

[!div class="nextstepaction"] Azure for Spring developers Spring Cloud Azure PostgreSQL Samples