Create a PostgreSQL single server read replica in Azure using Terraform

Deploying PostgreSQL single server in Terraform and wanting to create a read replica? In this blog post I will show you how, it is rather straight-forward to setup with Terraform.

Lets begin by looking at the terraform to create PostgreSQL single server; will follow on with showing how to add read-replica with terraform.

Terraform for PostgreSQL single server

The below will create:

  • resource group
  • PostgreSQL single server
  • PostgreSQL single server database (do not store credentials in plain text – only doing this for example only)
resource "azurerm_resource_group" "rg" {
  name     = "tamops-postgres"
  location = "West Europe"
}

resource "azurerm_postgresql_server" "postgresserver" {
  name                = "postgresql-tamops"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  sku_name = "B_Gen5_2"

  storage_mb                   = 5120
  backup_retention_days        = 7
  geo_redundant_backup_enabled = false
  auto_grow_enabled            = true

  administrator_login          = "psqladmin"
  administrator_login_password = "H@Sh1CoR3!"
  version                      = "9.5"
  ssl_enforcement_enabled      = true
}

resource "azurerm_postgresql_database" "db" {
  name                = "dbtest"
  resource_group_name = azurerm_resource_group.rg.name
  server_name         = azurerm_postgresql_server.postgresserver.name
  charset             = "UTF8"
  collation           = "en-GB"
}

Screenshot showing Azure PostgreSQL database created using Terraform in Azure Portal

Adding the read-replica

Adding a read-replica, all you need to do is create another resource azurerm_postgresql_server with two additional arguments.

  • create_mode = "Replica"

The creation mode. Can be used to restore or replicate existing servers. Possible values are DefaultReplicaGeoRestore, and PointInTimeRestore. Defaults to Default.

  • creation_source_server_id = azurerm_postgresql_server.postgresserver.id

For creation modes other than Default, the source server ID to use.

resource "azurerm_postgresql_server" "replica" {
  name                = "postgresql-replica"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  sku_name = "B_Gen5_2"

  storage_mb                   = 5120
  backup_retention_days        = 7
  geo_redundant_backup_enabled = false
  auto_grow_enabled            = true

  administrator_login          = "psqladmin"
  administrator_login_password = "H@Sh1CoR3!"
  version                      = "9.5"
  ssl_enforcement_enabled      = true

  create_mode               = "Replica"
  creation_source_server_id = azurerm_postgresql_server.postgresserver.id

}

Very straight-forward, if you need another replica, you would copy the above again & again 🙂

Screenshot showing Azure PostgreSQL databases created using Terraform in Azure Portal

Checking the main database replication settings, we can see the replica configuration has been updated with the additional replica

Screenshot showing Azure PostgreSQL database replication settings in Azure Portal

Github repository for this setup.

To run the terraform in repo above (when inside terraform folder):

terraform init
terraform apply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s