I'm trying to run this command for many tables (one table at a time) in Go 1.9:
COPY (select row_to_json(foo) FROM (SELECT * FROM bar) foo ) TO '/tmp/bar.json';
Is this even possible? It seems with lib/pg, it is not. With go-pg, I keep running out of memory because it buffers it all into memory first.
Doing this from the command prompt works fine. I'd rather use Go's PG libs than have it run it at command prompt.
In short, I'm trying to dump entire tables into JSON in their own files.
Has anyone done this successfully?
Thank you!
EDIT:
Since lb/pg didn't support this at all, I'm using pg-go. Here is the code:
var buf bytes.Buffer
_, err := db.CopyTo(&buf, "COPY (select row_to_json(foo) FROM (SELECT * FROM bar) r ) TO '/tmp/bar.json'")
if err != nil {
panic(err)
}