title | description | keywords | ms.topic | service | ms.service | ms.date | ms.custom |
---|---|---|---|---|---|---|---|
Deploy a PostgreSQL Flexible Server Database using Terraform |
Learn how to deploy a PostgreSQL Flexible Server Database using Terraform |
azure, devops, terraform, postgresql, flexible server, database |
how-to |
postgresql |
postgresql |
10/26/2023 |
devx-track-terraform |
Article tested with the following Terraform and Terraform provider versions:
[!INCLUDE Terraform abstract]
This article shows how to deploy a PostgreSQL Flexible Server Database using Terraform.
In this article, you learn how to:
[!div class="checklist"]
- Create an Azure resource group using azurerm_resource_group
- Create an Azure virtual network (VNet) using azurerm_virtual_network
- Create an Azure Network Security Group (NSG) using azurerm_network_security_group
- Create an Azure subnet azurerm_subnet
- Create an Azure subnet Network Security Group (NSG) using azurerm_subnet_network_security_group_association
- Define a private DNS zone within an Azure DNS using azurerm_private_dns_zone
- Define a private DNS zone VNet link using using azurerm_private_dns_zone_virtual_network_link
- Deploy an Azure PostgreSQL Flexible Server on which the database runs using azurerm_postgresql_flexible_server
- Instantiate an Azure PostgreSQL database using azurerm_postgresql_flexible_server_database
Note
The example code in this article is located in the Azure Terraform GitHub repo.
[!INCLUDE open-source-devops-prereqs-azure-subscription.md]
[!INCLUDE configure-terraform.md]
-
Create a directory in which to test and run the sample Terraform code and make it the current directory.
-
Create a file named
providers.tf
and insert the following code:[!code-terraformmaster]
-
Create a file named
main.tf
and insert the following code to deploy the PostgreSQL Flexible Server on which the database runs.[!code-terraformmaster]
-
Create a file named
postgresql-fs-db.tf
and insert the following code to instantiate the database:[!code-terraformmaster]
-
Create a file named
variables.tf
and insert the following code:[!code-terraformmaster]
-
Create a file named
outputs.tf
and insert the following code to output the resource group name, Azure PostgreSQL server name, and Azure PostgreSQL database name:[!code-terraformmaster]
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-plan.md]
[!INCLUDE terraform-apply-plan.md]
Run az postgres flexible-server db show to display the Azure PostgreSQL database.
az postgres flexible-server db show --resource-group <resource_group_name> --server-name <server_name> --database-name <database_name>
Key points:
- The values for the
<resource_group_name>
,<server_name>
, and<database_name>
are displayed in theterraform apply
output.
Run Get-AzPostgreSqlFlexibleServerDatabase to display the Azure PostgreSQL database.
Get-AzPostgreSqlFlexibleServerDatabase -ResourceGroupName <resource_group_name> -ServerName <server_name> -Name <database_name>
Key points:
- The values for the
<resource_group_name>
,<server_name>
, and<database_name>
are displayed in theterraform apply
output.
[!INCLUDE terraform-plan-destroy.md]
Troubleshoot common problems when using Terraform on Azure
[!div class="nextstepaction"] Learn more about PostgreSQL Flexible Server