You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Terraform local-exec执行PostgreSQL命令时遇文件未找到错误

解决Terraform执行PostgreSQL命令时的“The system cannot find the file specified.”错误

问题场景

  • 已成功创建PostgreSQL实例
  • 命令提示符中运行psql命令可正常连接数据库
  • Terraform中配置基础连接代码能成功连接服务器
  • 执行具体PostgreSQL命令(如建表、数据插入等)时,反复报错:

    The system cannot find the file specified.

可行解决方案

  1. 显式指定psql执行路径
    在Terraform的postgresql_exec资源中,通过psql_path参数指定psql.exe的完整安装路径,示例代码:

    resource "postgresql_exec" "init_test_table" {
      connection {
        host     = "your_db_host"
        port     = 5432
        database = "target_db"
        username = "db_user"
        password = "db_password"
      }
    
      # 根据你的PostgreSQL版本和安装路径调整
      psql_path = "C:\\Program Files\\PostgreSQL\\15\\bin\\psql.exe"
      command   = "CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name VARCHAR(50));"
    }
    
  2. 配置系统环境变量(无需硬编码路径)
    将PostgreSQL安装目录下的bin文件夹路径添加到系统PATH环境变量中,之后重启Terraform的执行终端(如PowerShell、CMD),确保环境变量生效,Terraform即可自动找到psql命令。

内容的提问来源于stack exchange,提问作者Ravi Khambhati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:19:55