Azure PostgreSQL弹性服务器Terraform配置pg_cron.jobs失败问题
在Azure PostgreSQL弹性服务器中安装pg_cron扩展后,使用azurerm Terraform provider配置pg_cron.jobs时遇到内部服务器错误。
配置代码
locals { default_pg_parameters = { "azure.extensions" = "pg_cron" "pg_cron.jobs" = "SELECT cron.schedule_in_database(job_name:='xxx_yy_test', schedule:='1 12 1,15 * *', command:=$$select * from all$$, database:='postgres');" } } resource "azurerm_postgresql_flexible_server_configuration" "cron_job" { for_each = local.default_pg_parameters name = each.key server_id = azurerm_postgresql_flexible_server.example.id value = each.value depends_on = [azurerm_postgresql_flexible_server.example] }
错误信息
Error: updating Configuration (Subscription: "xxxx-cccc-sasdf-asdfsd-asdfasd-12132"
Resource Group Name: "xzxcv-rg"
Flexible Server Name: "example-postgres-flexi"
Configuration Name: "pg_cron.jobs"): polling after Update: polling failed: the Azure API returned the following error:Status: "InternalServerError"
Code: ""
Message: "An unexpected error occured while processing the request. Tracking ID: '9131c5b9-e371-425b-834e-ac2027cce3a6'"
Activity Id: ""API Response:
----[start]----
{"name":"3777d618-8e1e-47e7-956a-4f7b0a04c49e","status":"Failed","startTime":"2024-07-31T10:28:28.507Z","error":{"code":"InternalServerError","message":"An unexpected error occured while processing the request. Tracking ID: '9131c5b9-e371-425b-834e-ac2027cce3a6'"}}
-----[end]-----with main.postgres_flexible_dbserver["1"].azurerm_postgresql_flexible_server_configuration.this["pg_cron.jobs"],
可能原因及解决方法
1. SQL语法或参数格式问题
- 问题点:
pg_cron.jobs参数对SQL格式要求严格,$$分隔符可能触发Azure端解析异常;同时select * from all是无效语句,PostgreSQL中不存在名为all的系统表。 - 解决方法:
- 将
$$替换为单引号转义,修正命令中的无效表名,示例:"pg_cron.jobs" = "SELECT cron.schedule_in_database(job_name:='xxx_yy_test', schedule:='1 12 1,15 * *', command:='select * from pg_catalog.pg_tables', database:='postgres');"
- 将
2. 资源依赖顺序问题
- 问题点:虽然配置了
depends_on,但pg_cron扩展可能未完全初始化就开始配置jobs,导致服务器无法识别cron.schedule_in_database函数。 - 解决方法:
- 拆分资源配置,先单独启用扩展,再配置jobs并添加显式依赖:
resource "azurerm_postgresql_flexible_server_configuration" "pg_cron_extension" { name = "azure.extensions" server_id = azurerm_postgresql_flexible_server.example.id value = "pg_cron" depends_on = [azurerm_postgresql_flexible_server.example] } resource "azurerm_postgresql_flexible_server_configuration" "cron_job" { name = "pg_cron.jobs" server_id = azurerm_postgresql_flexible_server.example.id value = "SELECT cron.schedule_in_database(job_name:='xxx_yy_test', schedule:='1 12 1,15 * *', command:='select * from pg_catalog.pg_tables', database:='postgres');" depends_on = [azurerm_postgresql_flexible_server_configuration.pg_cron_extension] }
- 拆分资源配置,先单独启用扩展,再配置jobs并添加显式依赖:
3. Azure API临时故障
- 问题点:InternalServerError可能是Azure服务端临时问题(如节点故障、服务升级)。
- 解决方法:
- 等待10-15分钟后重新执行
terraform apply。 - 用Azure CLI手动测试配置,验证是否能成功:
az postgres flexible-server configuration set --resource-group xzxcv-rg --server-name example-postgres-flexi --name pg_cron.jobs --value "SELECT cron.schedule_in_database(job_name:='xxx_yy_test', schedule:='1 12 1,15 * *', command:='select * from pg_catalog.pg_tables', database:='postgres');"
- 等待10-15分钟后重新执行
4. Terraform Provider版本问题
- 问题点:旧版azurerm provider对
pg_cron.jobs参数的处理可能存在bug。 - 解决方法:
- 更新provider到最新稳定版,在
versions.tf中指定:terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.0.0" } } }
- 更新provider到最新稳定版,在
内容的提问来源于stack exchange,提问作者Avi

