使用sqlc生成Go代码时遇PostgreSQL引擎不支持Windows错误求排查
问题描述
在Windows环境下使用sqlc生成Go代码时,报错:the PostgreSQL engine does not support Windows,已通过Docker Alpine镜像部署PostgreSQL服务,相关配置文件如下:
sql.yaml配置
version: "1" packages: - name: "db" path: "./db/sqlc" queries: "./db/query/" schema: "./db/migration/" engine: "postgresql" emit_json_tags: true emit_prepared_queries: false emit_interface: false emit_exact_table_names: false
查询语句(account.sql)
-- name CreateAccount: one INSERT INTO accounts ( owner, balance, currency ) VALUES( $1,$2,$3 ) RETURNING *;
Schema定义
CREATE TABLE "accounts" ( "id" bigserial PRIMARY KEY, "owner" varchar NOT NULL, "balance" bigint NOT NULL, "currency" varchar NOT NULL, "created_at" timestamptz NOT NULL DEFAULT (now()) ); CREATE TABLE "entries" ( "id" bigserial PRIMARY KEY, "account_id" bigint NOT NULL, "amount" bigint NOT NULL, "created_at" timestamptz NOT NULL DEFAULT (now()) ); CREATE TABLE "transfers" ( "id" bigserial PRIMARY KEY, "from_account_id" bigint NOT NULL, "to_account_id" bigint NOT NULL, "amount" bigint NOT NULL, "created_at" timestamptz NOT NULL DEFAULT (now()) ); ALTER TABLE "entries" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); ALTER TABLE "transfers" ADD FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("id"); ALTER TABLE "transfers" ADD FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("id"); CREATE INDEX ON "accounts" ("owner");
解决方案
这个错误的核心原因是sqlc的PostgreSQL代码生成器依赖libpg库,该库在Windows原生环境中无官方支持,和你是否用Docker运行PostgreSQL服务无关。可通过以下两种方式解决:
方式一:使用WSL2运行sqlc
- 安装WSL2并配置Linux发行版(如Ubuntu)
- 在WSL2终端执行安装命令:
curl -sSf https://raw.githubusercontent.com/sqlc-dev/sqlc/main/install.sh | sh - 进入项目目录,运行
sqlc generate命令
方式二:使用Docker镜像运行sqlc
直接通过官方sqlc镜像执行生成命令,绕开Windows本地环境限制:docker run --rm -v "$(pwd):/src" -w /src kjconroy/sqlc generate
你的sql.yaml、查询语句和Schema定义均无问题,无需修改。
内容的提问来源于stack exchange,提问作者Pramod
相关产品推荐
相关产品推荐

