Search code examples
goenvironment-variablesbackendsqlcgoose

Golang unable to "sqlc generate"


I am using windows and Ubuntu WSL but get this error when I try to sqlc generate.

sqlc generate
# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
export CGO_ENABLED=1 && sqlc generate
# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.

When I try go env command, but it's in my environment but still not able to sqlc generate

go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
.
.
.
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
.
.
.
CGO_ENABLED='1'
GOMOD='/mnt/c/Users/User/Desktop/Golang/main/go.mod'

Solution

  • I was able to duplicate this on a fresh install of WSL2 Ubuntu 22.04 (running on Win 11). With the fresh image I installed Go (1.21.4) & sqlc (v1.23.0) then ran sqlc generate (using the example repo from the tutorial mentioned in the question) and got the error:

    # package database
    sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
    sql/schema/002_users_apikey.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
    sql/schema/003_feeds.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
    

    Interestingly everything worked fine under Ubuntu 20.04 (also on WSL2).

    There is an issue with the compatibility of CGO enabled binaries across Ubuntu 20.04 and 22.04 (did not look at the issue in any real detail!); so it seemed worth trying to re-install sqlc with CGO enabled:

    go env -w CGO_ENABLED=1
    sudo apt update
    sudo apt install gcc
    go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
    

    After doing this sqlc worked (successfully generated the go files for the 6-createfeed example without issue).

    Note: As this worked I thought I'd add comments to any similar sqlc issues and came across this comment suggesting the same basic solution (did not spot this when I looked last night).