How to insert multi-dimensional arrays using pgx/v5 #2263
Answered
by
jackc
pacifico3000
asked this question in
Q&A
|
Hello! In my code I have two-dimensional slice of type [][]int32 which i cannot insert into postgres table. The table is like this: Doing something like: and an error looks like It is working if I use lib/pq: |
Answered by
jackc
Feb 25, 2025
Replies: 1 comment 3 replies
|
Works for me. package main
import (
"context"
"log"
"os"
"github.com/jackc/pgx/v5"
)
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, "create temporary table t (id bigint primary key, ary int[][]);")
if err != nil {
log.Fatal(err)
}
_, err = conn.Exec(ctx, "insert into t values (1, $1);", [][]int{{1, 2}, {3, 4}})
if err != nil {
log.Fatal(err)
}
var ary [][]int
err = conn.QueryRow(ctx, "select ary from t where id = 1;").Scan(&ary)
if err != nil {
log.Fatal(err)
}
log.Printf("ary: %v", ary)
} |
3 replies
Answer selected by
pacifico3000
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for me.