ERROR: 'with' expected (SQLSTATE 00000) 0
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
**Describe the bug**
Hi, I am trying to move data from mongodb to postgres using pgx copy from however I am getting an error and the copy count = 0 ERROR: 'with' expected (SQLSTATE 00000)
**To Reproduce**
Steps to reproduce the behavior:
If possible, please provide runnable example such as:
```go
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/jackc/pgx/v5"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type Category struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Name struct {
En string `json:"en"`
Ar string `json:"ar"`
} `json:"name"`
Company struct {
Name string `json:"name"`
ID string `json:"_id"`
} `json:"company"`
CompanyRef primitive.ObjectID `json:"companyRef"`
Image string `json:"image"`
Description string `json:"description"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func categories(client *mongo.Client, psql *pgx.Conn) {
db := client.Database("test")
collection := db.Collection("categories")
filter := bson.M{}
cursor, err := collection.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
var results []Category
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
copyCount, err := psql.CopyFrom(
context.Background(),
pgx.Identifier{"categories"},
[]string{"categoryNameEn", "categoryNameAr", "companyNameEn", "companyRef", "description", "createdAt", "categoryRef"},
pgx.CopyFromSlice(len(results), func(i int) ([]any, error) {
return []any{results[i].Name.En, results[i].Name.Ar, results[i].Company.Name, results[i].CompanyRef.String(), results[i].Description, results[i].CreatedAt.Format(time.RFC3339), results[i].ID.Hex()}, nil
}),
)
fmt.Println(err, copyCount)
fmt.Println("Bulk insert completed successfully.")
}
```
Please run your example with the race detector enabled. For example, `go run -race main.go` or `go test -race`.
**Expected behavior**
Should be able to copy without any error
**Actual behavior**

**Version**
- Go: `$ go version` -> go version go1.22.1 linux/amd64
- PostgreSQL: `$ psql --no-psqlrc --tuples-only -c 'select version()'` -> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]
- pgx: `$ grep 'github.com/jackc/pgx/v[0-9]' go.mod` -> v5
**Additional context**
Add any other context about the problem here.
Contributor guide
Research direction
Start with the provided categories function and its pgx.CopyFrom call, then run the example with the Go race detector as requested. Reproduce the PostgreSQL error using the shown MongoDB data and copy arguments; done means identifying the cause of the rejected copy operation and verifying that the corrected example copies rows successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, mongodb, postgres
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100