title | description | author | ms.date | ms.author | ms.topic | ms.custom | zone_pivot_group_filename | zone_pivot_groups |
---|---|---|---|---|---|---|---|---|
Use Spring Data JDBC with Azure Database for PostgreSQL |
Learn how to use Spring Data JDBC with an Azure Database for PostgreSQL database. |
KarlErickson |
02/22/2023 |
hangwan |
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 an Azure Database for PostgreSQL database using Spring Data JDBC.
JDBC is the standard Java API to connect to traditional relational databases.
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.
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-jdbc-postgresql.
[!INCLUDE spring-data-azure-postgresql-flexible-server-setup.md]
To store data from Azure Database for PostgreSQL using Spring Data JDBC, 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.springframework.jdbc.core=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.sql.init.mode=always
logging.level.org.springframework.jdbc.core=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.sql.init.mode=always
[!WARNING] The configuration property
spring.sql.init.mode=always
means that Spring Boot will automatically generate a database schema, using the schema.sql file that you'll create next, each time the server is started. This feature is great for testing, but remember that it will delete your data at each restart, so you shouldn't use it in production.
::: 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.
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-jdbc-postgresql.
[!INCLUDE spring-data-azure-postgresql-single-server-setup.md]
To store data from Azure Database for PostgreSQL using Spring Data JDBC, 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.springframework.jdbc.core=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.sql.init.mode=always
logging.level.org.springframework.jdbc.core=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.sql.init.mode=always
[!WARNING] The configuration property
spring.sql.init.mode=always
means that Spring Boot will automatically generate a database schema, using the schema.sql file that you'll create next, each time the server is started. This feature is great for testing, but remember that it will delete your data at each restart, so you shouldn't use it in production.
::: zone-end
-
Create the src/main/resources/schema.sql configuration file to configure the database schema, then add the following contents.
DROP TABLE IF EXISTS todo; CREATE TABLE todo (id SERIAL PRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN);
[!INCLUDE spring-data-jdbc-create-application.md]
[!INCLUDE deploy-to-azure-spring-apps]
[!div class="nextstepaction"] Azure for Spring developers Spring Cloud Azure PostgreSQL Samples