All Products
Search
Document Center

Terraform:Manage RDS PostgreSQL instance parameters using Terraform

Last Updated:Sep 22, 2026

Modify parameters and query parameter modification logs for an ApsaraDB RDS for PostgreSQL instance using Terraform.

Note

Run the sample code with a few clicks in Terraform Explorer.

Prerequisites

Before you begin, make sure you have:

  • An RDS instance in the Running state. To verify the status, check the status parameter value by following Query instance details, or log in to the ApsaraDB RDS console and check the instance status directly. To create an instance, see Create an RDS instance

  • A Resource Access Management (RAM) user with an AccessKey pair, rather than the root account. Following the principle of least privilege (PoLP), attach the following policy to the RAM user. For setup instructions, see Create a RAM user and Create an AccessKey pair

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeRouteTableList",
                    "vpc:DescribeVSwitchAttributes",
                    "vpc:DeleteVpc",
                    "vpc:DeleteVSwitch",
                    "vpc:CreateVpc",
                    "vpc:CreateVSwitch"
                ],
                "Resource": "*"
            },
            {
                "Action": "rds:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "dbs:*",
                "Resource": "acs:rds:*:*:*",
                "Effect": "Allow"
            },
            {
                "Action": "hdm:*",
                "Resource": "acs:rds:*:*:*",
                "Effect": "Allow"
            },
            {
                "Action": "dms:LoginDatabase",
                "Resource": "acs:rds:*:*:*",
                "Effect": "Allow"
            },
            {
                "Effect": "Allow",
                "Action": "ram:CreateServiceLinkedRole",
                "Resource": "*",
                "Condition": {
                    "StringEquals": {
                        "ram:ServiceName": [
                            "backupencryption.rds.aliyuncs.com"
                        ]
                    }
                }
            },
            {
                "Effect": "Allow",
                "Action": "bss:ModifyAgreementRecord",
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "bss:DescribeOrderList",
                    "bss:DescribeOrderDetail",
                    "bss:PayOrder",
                    "bss:CancelOrder"
                ],
                "Resource": "*"
            }
        ]
    }
  • A Terraform environment. Choose one of the following options:

Resources

Note

The following resources incur charges. Release or unsubscribe from resources you no longer need.

Resource

Description

alicloud_vpc

Creates a virtual private cloud (VPC)

alicloud_vswitch

Creates a vSwitch for the VPC

alicloud_db_instance

Creates an RDS instance

Modify parameters

Note

Some parameter changes trigger an immediate instance restart, which disconnects your application. To check whether a parameter requires a restart, see the Force Restart column on the Editable Parameters tab in the ApsaraDB RDS console. Schedule changes during off-peak hours and ensure your application reconnects automatically.

The following steps change the value of the authentication_timeout parameter from 60 to 120.

  1. Create a working directory and a main.tf configuration file inside it. Add the following configuration to main.tf:

    • variable "region" {
        default = "cn-heyuan"
      }
      
      provider "alicloud" {
        region = var.region
      }
      
      variable "zone_id" {
        default = "cn-heyuan-b"
      }
      
      variable "instance_type" {
        default = "pg.n2.2c.2m"
      }
      
      # Create a VPC.
      resource "alicloud_vpc" "main" {
        vpc_name   = "alicloud"
        cidr_block = "172.16.0.0/16"
      }
      
      # Create a vSwitch.
      resource "alicloud_vswitch" "main" {
        vpc_id     = alicloud_vpc.main.id
        cidr_block = "172.16.192.0/20"
        zone_id    = var.zone_id
      }
      
      # Create an RDS instance.
      # Default minor engine version: rds_postgres_1300_20240229
      resource "alicloud_db_instance" "instance" {
        engine               = "PostgreSQL"
        engine_version       = "13.0"
        instance_type        = var.instance_type
        instance_storage     = "30"
        instance_charge_type = "Postpaid"
        vswitch_id           = alicloud_vswitch.main.id
        # Modify parameters. Run terraform apply again after changes.
        parameters {
          name  = "authentication_timeout"
          value = "120"
        }
      }
  2. Initialize Terraform:

    terraform init

    A successful initialization returns output similar to:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Preview the changes:

    terraform plan
  4. Apply the configuration:

    terraform apply

    Enter yes when prompted and press Enter. A successful apply returns output similar to:

    alicloud_db_instance.instance: Modifying... [id=pgm-****]
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 10s elapsed]
    ...
    alicloud_db_instance.instance: Still modifying... [id=pgm-****, 3m10s elapsed]
    alicloud_db_instance.instance: Modifications complete after 3m10s [id=pgm-****]
    
    Apply complete!  Resources: 0 added, 1 changed, 0 destroyed.
  5. Verify the result.

    Run the terraform show command

    Run terraform plan to confirm no pending changes remain:

    terraform plan

    A result of No changes. Your infrastructure matches the configuration. confirms the parameter is applied. For a detailed state view, run:

    terraform show

    The output includes the applied parameter value:

    # alicloud_db_instance.instance:
    resource "alicloud_db_instance" "instance" {
        acl                            = null
        auto_upgrade_minor_version     = "Auto"
        ca_type                        = null
        category                       = "HighAvailability"
        client_ca_cert                 = (sensitive value)
        client_ca_enabled              = 0
        client_cert_revocation_list    = null
        client_crl_enabled             = 0
        connection_string              = "pgm-f8z03v853neh87k8.pg.rds.aliyuncs.com"
        connection_string_prefix       = "pgm-f8z03v853neh87k8"
        create_time                    = "2024-10-31T02:29:36Z"
        db_instance_ip_array_attribute = null
        db_instance_ip_array_name      = null
        db_instance_storage_type       = "cloud_essd"
        db_instance_type               = "Primary"
        db_param_group_id              = null
        db_time_zone                   = "Asia/Shanghai"
        deletion_protection            = false
        engine                         = "PostgreSQL"
        engine_version                 = "13.0"
        force_restart                  = false
        ha_config                      = "Auto"
        id                             = "pgm-f8z03v853neh87k8"
        instance_charge_type           = "Postpaid"
        instance_name                  = null
        instance_storage               = 30
        instance_type                  = "pg.n2.2c.2m"
        maintain_time                  = "18:00Z-22:00Z"
        manual_ha_time                 = null
        monitoring_period              = 300
        node_id                        = "32990438"
        period                         = 0
        port                           = "5432"
        private_ip_address             = "172.16.206.226"
        replication_acl                = null
        resource_group_id              = "rg-acfmykd63gtpfpa"
        security_group_ids             = []
        security_ip_mode               = "normal"
        security_ip_type               = null
        security_ips                   = [
            "127.0.0.1",
        ]
        server_cert                    = (sensitive value)
        server_key                     = null
        sql_collector_config_value     = 30
        sql_collector_status           = "Disabled"
        ssl_action                     = "Close"
        ssl_connection_string          = null
        ssl_status                     = null
        status                         = "Running"
        storage_auto_scale             = null
        storage_threshold              = 0
        storage_upper_bound            = 0
        target_minor_version           = "rds_postgres_1300_20240830"
        tcp_connection_type            = "LONG"
        tde_status                     = "Disabled"
        vpc_id                         = "vpc-f8zvtuuan0tjyas7muymk"
        vswitch_id                     = "vsw-f8zjvdbtv24927fmvrydl"
        whitelist_network_type         = null
        zone_id                        = "cn-heyuan-a"
    
        parameters {
            name  = "authentication_timeout"
            value = "120"
        }
    }

    Log on to the ApsaraDB RDS console

    To verify in the console, log in to the ApsaraDB RDS console and check the parameter value on the instance's parameters page. In the parameter list of the Editable Parameters tab, you can see that the Running Parameter Value of the authentication_timeout parameter has been changed from the default value 60 to 120.

Query parameter modification logs

  1. Add the following configuration to main.tf:

    ...
    resource "time_static" "example" {}
    
    data "alicloud_rds_modify_parameter_logs" "querylogs" {
      db_instance_id = alicloud_db_instance.instance.id
      start_time     = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "-12h"))
      end_time       = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "12h"))
    }

    The time_static resource records the current time. The query window covers 12 hours before and after that timestamp.

  2. Apply the configuration:

    terraform apply

    Enter yes when prompted and press Enter. A successful apply returns output similar to:

    alicloud_db_instance.instance: Refreshing state... [id=pgm-****]
    data.alicloud_rds_modify_parameter_logs.querylogs: Reading...
    alicloud_db_backup_policy.instance: Refreshing state... [id=pgm-****]
    data.alicloud_rds_modify_parameter_logs.querylogs: Read complete after 0s [id=6335****]
    
    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
    
    Apply complete!  Resources: 0 added, 0 changed, 0 destroyed.
  3. Verify the result.

    Run the terraform show command

    Run terraform show to display the parameter modification logs:

    terraform show

    The output lists each modification with its old value, new value, and status:

    # data.alicloud_rds_modify_parameter_logs.querylogs:
    data "alicloud_rds_modify_parameter_logs" "querylogs" {
        db_instance_id = "pgm-f8z03v853neh87k8"
        end_time       = "2024-10-31T23:59Z"
        id             = "6722ee94"
        logs           = [
            {
                modify_time         = "1730342339000"
                new_parameter_value = "120"
                old_parameter_value = "60"
                parameter_name      = "authentication_timeout"
                status              = "Applied"
            },
        ]
        start_time     = "2024-10-31T00:00Z"
    }

Clean up resources

To release all resources created above, run:

terraform destroy

For details about terraform destroy, see Common Terraform commands.

Complete sample code

Note

Run the sample code with a few clicks in Terraform Explorer.

Sample code

variable "region" {
  default = "cn-heyuan"
}

variable "zone_id" {
  default = "cn-heyuan-a"
}

variable "instance_type" {
  default = "pg.n2.2c.2m"
}

provider "alicloud" {
  region = var.region
}

# Create a VPC.
resource "alicloud_vpc" "main" {
  vpc_name   = "alicloud"
  cidr_block = "172.16.0.0/16"
}

# Create a vSwitch.
resource "alicloud_vswitch" "main" {
  vpc_id     = alicloud_vpc.main.id
  cidr_block = "172.16.192.0/20"
  zone_id    = var.zone_id
}

# Create an RDS instance.
resource "alicloud_db_instance" "instance" {
  engine               = "PostgreSQL"
  engine_version       = "13.0"
  instance_type        = var.instance_type
  instance_storage     = "30"
  instance_charge_type = "Postpaid"
  vswitch_id           = alicloud_vswitch.main.id
  # Modify parameters. Run terraform apply again after changes.
  parameters {
    name  = "authentication_timeout"
    value = "120"
  }
}

# Record the current time for the log query window.
resource "time_static" "example" {}

# Query parameter modification logs.
data "alicloud_rds_modify_parameter_logs" "querylogs" {
  db_instance_id = alicloud_db_instance.instance.id
  start_time     = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "-12h"))
  end_time       = formatdate("YYYY-MM-DD'T'hh:mm'Z'", timeadd(time_static.example.rfc3339, "12h"))
}

For more examples, see the quickstarts repository for your service.

References

What's next