使用pgx.copyfrom或pgx插入GIS几何后几何。

发布于 2025-01-28 03:57:19 字数 1520 浏览 2 评论 0原文

我找到了从

使用pgx.copyfromrows,

rows := [][]interface{}{
        {"John", "ST_SetSRID(ST_MakePoint(1.23,2.34), 4326)"},
        {"Jane", "ST_SetSRID(ST_MakePoint(1.10,2.12), 4326)"},
    }

    // GetSession return a *pgxpool.Pool
    copyCount, err := postgres.GetSession().CopyFrom(
        context.Background(),
        pgx.Identifier{"test_db"},
        []string{"first_name", "location"},
        pgx.CopyFromRows(rows),
    )

这使我

"ERROR: Invalid endian flag value encountered. (SQLSTATE XX000)"

使用pgx.batch:

batch := &pgx.Batch{}
    batch.Queue("insert into people(first_name, location) values($1, $2)", "Bob", "ST_SetSRID(ST_MakePoint(1.23,1.34), 4326)")
    batch.Queue("insert into people(first_name, location) values($1, $2)", "John", "ST_SetSRID(ST_MakePoint(1.23,1.34), 4326)")
    batchResult := postgres.GetSession().SendBatch(context.Background(), batch)
    _, err := batchResult.Exec()
    if err != nil {
        return rest_errors.NewInternalServerError("Error processing batch insert", err)
    }
    batchResult.Close()

我得到这个错误

Error processing batch insert - parse error - invalid geometry (SQLSTATE XX000)

db创建脚本

CREATE TABLE IF NOT EXISTS public.test_db
(
    first_name text COLLATE pg_catalog."default",
    location geometry
)

非常感谢您

I've found the way to make a bulk insert with classic postgres types from this post and it works like a charm.
But for whatever reason, i struggle to make it work when trying to insert geometry points:

using pgx.CopyFromRows

rows := [][]interface{}{
        {"John", "ST_SetSRID(ST_MakePoint(1.23,2.34), 4326)"},
        {"Jane", "ST_SetSRID(ST_MakePoint(1.10,2.12), 4326)"},
    }

    // GetSession return a *pgxpool.Pool
    copyCount, err := postgres.GetSession().CopyFrom(
        context.Background(),
        pgx.Identifier{"test_db"},
        []string{"first_name", "location"},
        pgx.CopyFromRows(rows),
    )

this gives me back

"ERROR: Invalid endian flag value encountered. (SQLSTATE XX000)"

using pgx.Batch:

batch := &pgx.Batch{}
    batch.Queue("insert into people(first_name, location) values($1, $2)", "Bob", "ST_SetSRID(ST_MakePoint(1.23,1.34), 4326)")
    batch.Queue("insert into people(first_name, location) values($1, $2)", "John", "ST_SetSRID(ST_MakePoint(1.23,1.34), 4326)")
    batchResult := postgres.GetSession().SendBatch(context.Background(), batch)
    _, err := batchResult.Exec()
    if err != nil {
        return rest_errors.NewInternalServerError("Error processing batch insert", err)
    }
    batchResult.Close()

I get this error

Error processing batch insert - parse error - invalid geometry (SQLSTATE XX000)

db creation script

CREATE TABLE IF NOT EXISTS public.test_db
(
    first_name text COLLATE pg_catalog."default",
    location geometry
)

Thank you so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文