w3resource

Guide to Azure PostgreSQL Flexible Server Deployment


Deploying PostgreSQL Flexible Server Using azurerm_postgresql_flexible_server

The azurerm_postgresql_flexible_server is a Terraform resource used to deploy and manage a PostgreSQL Flexible Server on Microsoft Azure. Flexible Server is a versatile PostgreSQL deployment option that offers greater control over server configuration, scaling, and maintenance. It supports features like zone redundancy, customizable storage, and automated backups.

This guide walks you through the process of creating a PostgreSQL Flexible Server with Terraform, explaining its syntax, key attributes, and an example configuration.


Syntax:

resource "azurerm_postgresql_flexible_server" "example" {
  name                = "example-server"             # Server name
  location            = "East US"                   # Azure region
  resource_group_name = azurerm_resource_group.example.name
  administrator_login = "adminuser"                 # Admin username
  administrator_password = "StrongP@ssw0rd"          # Admin password

  sku_name   = "Standard_B2ms"                      # Instance size
  storage_mb = 32768                                # Storage size in MB
  version    = "13"                                 # PostgreSQL version

  high_availability {
    mode = "ZoneRedundant"                          # HA mode (Optional)
  }

  backup {
    retention_days = 7                              # Backup retention period
  }

  network {
    delegated_subnet_id = azurerm_subnet.example.id # Subnet configuration
    private_dns_zone_id = azurerm_private_dns_zone.example.id
  }

  tags = {                                          # Metadata tags
    environment = "production"
  }
}

Key Attributes

    1. name: The unique name of the PostgreSQL Flexible Server.

    2. sku_name: Defines the pricing tier and instance type (e.g., Standard_B2ms).

    3. version: PostgreSQL version (e.g., 13, 14).

    4. high_availability: Configures zone redundancy or disabled HA.

    5. backup: Manages backup retention and policy.

    6. network: Specifies subnet and private DNS zone integration.

Step-by-Step Example

Step 1: Create a Resource Group

Code:

# Create a resource group for the server
resource "azurerm_resource_group" "example" {
  name     = "example-rg"        # Resource group name
  location = "East US"           # Region
}

Step 2: Define the Virtual Network and Subnet

Code:

# Define a virtual network
resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]            # Address space
}

# Define a subnet within the VNet
resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]           # Subnet range
}

Step 3: Configure and Deploy the PostgreSQL Flexible Server

Code:

# Deploy the PostgreSQL Flexible Server
resource "azurerm_postgresql_flexible_server" "example" {
  name                = "example-server"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  administrator_login = "adminuser"
  administrator_password = "StrongP@ssw0rd"

  sku_name   = "Standard_B2ms"
  storage_mb = 32768
  version    = "13"

  high_availability {
    mode = "ZoneRedundant"
  }

  backup {
    retention_days = 7
  }

  network {
    delegated_subnet_id = azurerm_subnet.example.id
  }

  tags = {
    environment = "production"
  }
}

Explanation:

    1. Resource Group: Acts as a logical container for resources.

    2. Virtual Network and Subnet: Ensures that the server is accessible only within the defined network.

    3. High Availability: Enables redundancy across availability zones for better resilience.

    4. Backup Configuration: Automates daily backups with a retention period.

    5. Private Access: Uses a subnet and private DNS zone to restrict public access.

Benefits of PostgreSQL Flexible Server

  • Customizable Configuration: Tailor settings like storage and high availability.
  • Scaling Options: Adjust compute resources on demand.
  • Zone Redundancy: Protects against data center failures.
  • Cost Management: Optimized pricing for workloads.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.