# PITR in Terraform

**URL:** <https://discuss.hashicorp.com/t/pitr-in-terraform/53617>\
**Category:** Azure\
**Created:** [May 9, 2023, 11:17pm UTC](https://discuss.hashicorp.com/t/pitr-in-terraform/53617 "2023-05-09T23:17:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wshao12](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/wshao12/32/34596_2.png) [@wshao12](https://discuss.hashicorp.com/u/wshao12)\
**Post date:** [May 9, 2023, 11:17pm UTC](https://discuss.hashicorp.com/t/pitr-in-terraform/53617/1 "2023-05-09T23:17:04Z")

</div>

I want to use [azurerm\_postgresql\_server](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_server) module for recovery from backup.  
This module has [`restore_point_in_time`](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_server#restore_point_in_time) and  
[https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql\_server#creation\_source\_server\_id](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_server#creation_source_server_id)  
It also has several required fields.  
The code below is to restore a postgres instance. You can see, it is very similar to creating a brand new instance except using above two fields.

Is there a simpler way to do that, instead of duplicating most of the code?

```auto
resource "azurerm_postgresql_server" "pg_server_abc_restore_2023_05_08" {
  administrator_login = lookup(local.pg_instances["abc"], "administrator_login", "dba")
  administrator_login_password = module.pg_passwords["abc"].secret
  auto_grow_enabled = lookup(local.pg_instances["abc"], "auto_grow_enabled", true)
  backup_retention_days = lookup(local.pg_instances["abc"], "backup_retention_days", "7")
 
 create_mode = "PointInTimeRestore"
 creation_source_server_id = azurerm_postgresql_server.this["abc"].id
  
geo_redundant_backup_enabled = lookup(local.pg_instances["abc"], "geo_redundant_backup_enabled", substr(local.pg_instances["abc"].sku_name, 0, 2) != "B_") # Basic tier does not support geo redundant backups 
  location = lookup(local.pg_instances["abc"], "region", var.region)
  name = join("-", [lookup(local.pg_instances["abc"], "name", "${local.longname}-pg-abc"), "recovery"])
  public_network_access_enabled = true
  resource_group_name = lookup(local.pg_instances["abc"], "resource_group_name", azurerm_resource_group.this.name)
  restore_point_in_time = "2023-05-01T06:20:00Z" 
  sku_name = local.pg_instances["abc"].sku_name
  ssl_enforcement_enabled = true
  ssl_minimal_tls_version_enforced = lookup(local.pg_instances["abc"], "ssl_minimal_tls_version_enforced", "TLS1_2")
  storage_mb = local.pg_instances["abc"].storage_mb
  version = lookup(local.pg_instances["abc"], "version", "11")
  tags = local.tags,
   
  lifecycle {
    ignore_changes = [
      storage_mb,
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![AndrewCi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/andrewci/32/43616_2.png) [@AndrewCi](https://discuss.hashicorp.com/u/AndrewCi)\
**Post date:** [December 6, 2024, 3:40pm UTC](https://discuss.hashicorp.com/t/pitr-in-terraform/53617/2 "2024-12-06T15:40:15Z")

</div>

Hi Wshao! Unfortunately there isn’t a simpler approach for using PITR for TF with Azure PSQL Flexi Server. Your code snippet is an accurate example of how you’d perform the restore. It requires a separate server to be instantiated. I believe this is mostly driven by the fact that Azure doesn’t currently support in-place PITR recovery for the overall PSQL Flexi Server service. If you try to perform a PITR via the portal or CLI you’ll see that it behaves very similarly to how TF is doing it minus the fact that Azure is auto-populating most of the fields for you.

I did develop a solution to address state management while using PITR for Azure PSQL and published an article titled, “[Managing Terraform State while using Azure PostgreSQL Flexible Server Point-in-Time Recovery](https://andrewcitera.medium.com/managing-terraform-state-while-using-azure-postgresql-flexible-server-point-in-time-recovery-fa0b7a9f039a)”. Let me know if it’s helpful!
