How to do a PostgreSQL COPY with Node.JS?

4,370 views
Skip to first unread message

Victor Hooi

unread,
Nov 17, 2013, 7:43:54 PM11/17/13
to nod...@googlegroups.com
Hi,

PostgreSQL offers a COPY command, to move data efficient between the database and files.

So basically, you can bulk-export a SQL command into a local CSV command.

I come from the Python world, and there's the psycopg2 library, which offers copy_to() method to dump a SQL command straight into a local file-like object:


I'm new to Node.js, and I'm wondering if there's something similar for Node.JS - perhaps that can stream the resulting CSV file back?

I had a look at the node-postgres library:


and from what I can tell, it seems to operate on individual rows for a query result.

Is there an efficient way of bulk dumping a PostgreSQL table into Node.JS?

Cheers,
Victor

greelgorke

unread,
Nov 18, 2013, 3:57:35 AM11/18/13
to nod...@googlegroups.com

Victor Hooi

unread,
Nov 18, 2013, 6:23:12 AM11/18/13
to nod...@googlegroups.com
Hi,

I saw the author also has this repo:


I'm curious if anybody has experience with it, or how it compares to the node-postgres Client that greelgorke mentioned?

Cheers,
Victor

Peter Rust

unread,
Nov 18, 2013, 11:28:07 AM11/18/13
to nod...@googlegroups.com
The "brianc/node-postgres" library is the main pg library for node, but the author, BrianC, is concerned about it growing too bloated and has been working recently to spin off non-core functionality into 3rd party modules. node-pg-copy-streams is one of these:

Instead of adding a bunch more code to the already bloated node-postgres I am trying to make the internals extensible and work on adding edge-case features as 3rd party modules. This is one of those.

I don't have personal experience with either of these -- we just use child_process.spawn() to run the pg copy command directly (which is probably similar to what these do under the hood). I'm not sure if we tried the copy command in the "node-postgres" library and had trouble with it or if we didn't know about it or it wasn't available at the time.

-- peter
Reply all
Reply to author
Forward
0 new messages