sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
11.61k stars 746 forks source link

The PostgreSQL engine requires cgo. Please set CGO_ENABLED=1 #2149

Open m1ker1n opened 1 year ago

m1ker1n commented 1 year ago

Version

1.17.2

What happened?

sqlc was installed by this:

go install github.com/kyleconroy/sqlc/cmd/sqlc@latest

Used

sqlc generate

As result got messages that I should set CGO_ENABLED=1.

Expected to generate go code.

Relevant log output

output:
db/migrations/0001_init.up.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
db/migrations/0002_ad.up.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
db/migrations/0003_file.up.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
db/migrations/0004_ad_attachment.up.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
db/migrations/0005_messenger.up.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.

go env:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/admin/.cache/go-build"
GOENV="/home/admin/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/admin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/admin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.7"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/mnt/e/qfbi/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4211620203=/tmp/go-build -gno-record-gcc-switches"

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

cavator commented 1 year ago

i uninstall the Go install, and use only the snapd one, and worked like a charm

Assbomber commented 1 year ago

Was having the same issue. Installed homebrew for linux, then ran brew install sqlc. Now the sqlc generate works fine.

kyleconroy commented 1 year ago

@m1ker1n Can you give us a bit more information about the Go installation you're using? I'm confused because go env claims that CGO_ENABLED is set to true.

hamedtahmasebi commented 1 year ago

Same error here using windows WSL2 ubuntu 22.04 go version go1.20.5 linux/amd64

utsavo-afk commented 11 months ago

same issue WSL ubuntu 22.04 go version 1.20.6 linux/amd64 go install .../sqlc

my go env VGO_ENABLED=1 but it still keeps giving the same error that I should enable CGO_ENABLED flag

brlala commented 11 months ago

anyone found a workaround for this?

hamedtahmasebi commented 11 months ago

For anyone around here using WSL. I tried the command with sudo one day and it started working out of nowhere🤦‍♂️. might be as simple as that, try it.

Assbomber commented 11 months ago

Switch to docker version 🙌. Easy

brlala commented 11 months ago

Yeah, for anyone encountering this issue, instead of running go install, just download the binary directly. Works for me

teaampee commented 11 months ago

I encountered this issue on WSL and ChromeOS linux environment. the issue occurred when CGO_ENABLED was 0 when I installed sqlc and turning it on afterwards didn't help.

I was able to reproduce it here are some logs:


$ go env -w CGO_ENABLED=0
$ go env CGO_ENABLED
0
$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
$ go env -w CGO_ENABLED=1
$ sqlc generate
# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
sql/schema/002_users_apiKeys.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
$ go env CGO_ENABLED
1
$ rm ~/go/bin/sqlc
$ go env -w CGO_ENABLED=1
$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# github.com/sqlc-dev/sqlc/cmd/sqlc
/usr/bin/ld: warning: x86_64.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
$ sqlc generate
$ go env CGO_ENABLED
1

I also installed couple of libraries like build-essential and libc-dev before I recreated the issue not sure if it made a difference.

ilessy commented 10 months ago

It's simple to fix, first you enable CGO

go env -w CGO_ENABLED=1

then you install gcc

sudo apt install build-essential

last you install sqlc again

go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
HzTTT commented 8 months ago

/This problem will occur if you use go install to download. I uninstalled the original one and installed it using brew and it was no problem.