Connecting k6 to postgres DB

Hi, I’m trying to use to connect to a postgres database. I’m getting the following go error:

    ERRO[0001] panic: runtime error: invalid memory address or nil pointer dereference
goroutine 57 [running]:
runtime/debug.Stack(0xc000544808, 0xc000544828, 0xc0005447f8)
        runtime/debug/stack.go:24 +0x9f*VU).runFn.func1(0xc00036b950, 0xc000545bc0) +0x1ee
panic(0x1227480, 0x1f84790)
        runtime/panic.go:969 +0x1b9 +0x85
panic(0x1227480, 0x1f84790)
        runtime/panic.go:969 +0x1b9*vm).try.func1(0xc00057a000, 0x0, 0xc000545980, 0x0, 0x0, 0x0, 0xc000545a08) +0x5d3
panic(0x1227480, 0x1f84790)
        runtime/panic.go:969 +0x1b9
database/sql.(*Rows).Columns(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        database/sql/sql.go:2935 +0x5c*SQL).Query(0x21e29b0, 0xc002a0a1a0, 0xc002296000, 0x23, 0x0, 0x0, 0x0) +0x9b
reflect.callMethod(0xc002205c40, 0xc0005451c8, 0xc0005451b0)
        reflect/value.go:733 +0x1f6
reflect.methodValueCall(0xc002a0a1a0, 0xc002296000, 0x23, 0x0, 0x0, 0x0, 0xc001009bc8, 0xc000d0dae0, 0xc000545420, 0x4e02c7, ...)
        reflect/asm_amd64.s:35 +0x42, 0xc002205c40, 0x13, 0x13b4922, 0x4, 0xc001009b60, 0x2, 0x2, 0xc0004dd4a0, 0x41c9f8, ...)
        reflect/value.go:476 +0x8c7
reflect.Value.Call(0x11f1520, 0xc002205c40, 0x13, 0xc001009b60, 0x2, 0x2, 0xc001184738, 0x0, 0x0)
        reflect/value.go:337 +0xb9*Runtime).wrapReflectFunc.func1(0x16154e0, 0xc000fe60f0, 0xc00057a3f0, 0x2, 0x7, 0xc0003ff9e0, 0x1) +0x3c9*vm)._nativeCall(0xc00057a000, 0xc000e88b40, 0x2) +0x2c2, 0xc00057a000) +0xaeb*vm).run(0xc00057a000) +0x99*funcObject).call(0xc0029f5790, 0x1615da0, 0x21e2460, 0xc000d0da80, 0x1, 0x1, 0x0, 0x0, 0xc000644890, 0x41a0c5) +0x33a*funcObject).Call(...) +0x96*vm).try(0xc00057a000, 0xc000644a28, 0x0) +0x163, 0x21e2460, 0xc000d0da80, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0) +0x10b*VU).runFn(0xc00036b950, 0x1604340, 0xc001009a40, 0x1, 0xc001564e40, 0xc000d0da80, 0x1, 0x1, 0x0, 0x0, ...) +0x1b3*ActiveVU).RunOnce(0xc0015ae800, 0x0, 0x0) +0x226, 0xc00258e1e0, 0x15ea700, 0xc0015ae800, 0xc000d0d9d0) +0x5e, 0xc00036b950) +0x237
created by +0xc9c

Goja stack:
ERRO[0001] a panic occurred in VU code but was caught: runtime error: invalid memory address or nil pointer dereference  executor=per-vu-iterations scenario=default

I built k6 using the following commands

git clone
cd xk6
CGO_ENABLED=1 go run ./cmd/xk6/main.go build master \

And here’s the script I’m running:

import sql from 'k6/x/sql';

const connStr = "postgres://"

const db ='postgres', connStr);

export function setup() {

    // Create data here.


export function teardown() {



export default function () {

//   db.exec(

//     "INSERT INTO keyvalues (key, value) VALUES('plugin-name', 'k6-plugin-sql');"

//   );

  let results = sql.query(db, 'SELECT * FROM departments LIMIT 10;');

  for (const row of results) {

    console.log(`key: ${row.key}, value: ${row.value}`);



Can anyone help? It’s not possible to raise issues on the repo.


Hi there and thanks for reporting this. I enabled the Issues on the repo so now these kinds of issues can be reported directly on GitHub.

This extension, as most xk6 extensions, isn’t well tested or maintained, so we missed this issue with Postgres. Sorry about that.

I pushed a fix to better handle errors, so if you rebuild it with xk6 now you’ll get better visibility of any errors instead of that nasty panic stack trace.

It’s difficult to say what’s the particular error in your case, but from my quick testing I was getting pq: SSL is not enabled on the server, so I had to pass sslmode=disable in the connection string, since I was running Postgres in a local container.

Another error running sql.query() could be pq: relation "departments" does not exist, so you’ll have to run it again and hopefully see a more helpful message this time.

Let us know here or if you have another issue, feel free to open a new one in the repo. Just keep in mind that this is not officially supported by the k6 team, and is more of a proof-of-concept project. :wink:

1 Like

How to build it under windows I’m getting command CGO_enabled=1 not found when running
CGO_ENABLED=1 go run ./cmd/xk6/main.go build master

Use the commands here GitHub - k6io/xk6: Build k6 with plugins (you must have ‘go’ installed)


go get -u

xk6 build v0.30.0 --with

That will build the ‘k6’ executable (for k6 version 0.30.0) in the directory that you’re in.

Should work. I haven’t tried it.

1 Like

@Sergiu Besides having the Go toolchain installed, you also need a C compiler like gcc installed. I just tested on Windows 10 and this one worked fine: tdm-gcc. Make sure you enable “Add to PATH” in the installation options.

Then the way you specify environment variables is different on cmd.exe. This worked for me:

set GOOS=windows
set GOARCH=amd64
xk6 build v0.30.0 --with
1 Like

OK, thank you, I was able to build it locally by using this commands