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
–with GitHub - grafana/xk6-sql: k6 extension to load test RDBMSs (PostgreSQL, MySQL, MS SQL and SQLite3)

Use the commands here GitHub - grafana/xk6: Build k6 with extensions (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

I wanted to connect to the database, but when I run the script, it gives the following error:

How can I resolve this error?
I would be grateful for any help.

Hi @Sholpan ,

Given this error I would guess one of the following (in decreasing likelihood):

  1. You ran exactly the command above xk6 build v0.30.0 --with which unfortunately is now kind of broken as we did change some module paths and new versions of xk6 and modules (xk6-sql is updated) are not compatible with k6 pre v0.32.0 and vice versa. running xk6 build --with will fix it for you.
  2. You have an old version of xk6 in which case you can run go install
  3. You also could’ve not run the command or not then executed as ./k6 run .... but k6 run ... which would’ve not used the newly build k6.

Hope this helps you, and sorry for the inconvenience :frowning:

I have a new version of k6 installed v32.0.

Do I need to install an old version к6 to use xk6?

Do I need to install an old version к6 to use xk6?

Did you try running xk6 build --with ? (without the v0.30.0)

да, вышла следующая ошибка:

maybe I’m installing it incorrectly:

  1. I installed go
  2. I downloaded the binaries
  3. then I ran the command in this folder: xк6 build --with GitHub - grafana/xk6-sql: k6 extension to load test RDBMSs (PostgreSQL, MySQL, MS SQL and SQLite3)

It seems like you can’t open … can you try in a browser/using curl?

You might be behind a firewall or something that prevents you :(.

I think GOPROXY="direct" xk6 build --with should work for you

the browser opens, using curl no

I am on a corporate network

no, it didn’t work
thanks, I will continue to look for solutions

if i work with a local directory , could I replace the path?

Can you elaborate on which path? if you are talking about the extension one you can do
xk6 build --with where . means the current directory but you can use relative or absolute paths.

You can also change the k6 path as in

# Build using a k6 fork repository from a local path. The version must be omitted
# and the path must be absolute.
$ XK6_K6_REPO="$PWD/../../k6" xk6 build \

both of which are shown in the xk6 examples