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 |
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]
-
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"
- If you don't have one, create an Azure Database for PostgreSQL Flexible Server instance named
postgresqlflexibletest
and a database nameddemo
. For instructions, see Quickstart: Create an Azure Database for PostgreSQL - Flexible Server in the Azure portal.
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.
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]
To store data from Azure Database for PostgreSQL using Spring Data JPA, follow these steps to configure the application:
-
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"
- If you don't have one, create an Azure Database for PostgreSQL Single Server instance named
postgresqlsingletest
and a database nameddemo
. For instructions, see Quickstart: Create an Azure Database for PostgreSQL server by using the Azure portal.
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.
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]
To store data from Azure Database for PostgreSQL using Spring Data JPA, follow these steps to configure the application:
-
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]
[!div class="nextstepaction"] Azure for Spring developers Spring Cloud Azure PostgreSQL Samples