使用Terraform local-exec执行PostgreSQL命令时遇文件未找到错误
解决Terraform执行PostgreSQL命令时的“The system cannot find the file specified.”错误
问题场景
- 已成功创建PostgreSQL实例
- 命令提示符中运行
psql命令可正常连接数据库 - Terraform中配置基础连接代码能成功连接服务器
- 执行具体PostgreSQL命令(如建表、数据插入等)时,反复报错:
The system cannot find the file specified.
可行解决方案
显式指定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));" }配置系统环境变量(无需硬编码路径)
将PostgreSQL安装目录下的bin文件夹路径添加到系统PATH环境变量中,之后重启Terraform的执行终端(如PowerShell、CMD),确保环境变量生效,Terraform即可自动找到psql命令。
内容的提问来源于stack exchange,提问作者Ravi Khambhati
相关产品推荐
相关产品推荐

