Skip to content
Advertisement

Import sql file in node.js and execute against PostgreSQL

I’m looking for an efficient way to take a raw sql file and have it executed synchronously against a postgres database, akin to if you ran it through psql.

I have an sql file which creates all databases, imports data, etc. I need to execute this using node.js but cannot find any module which does this automatically. For the node.js application itself, we use node-postgres (‘pg’), knex.js and bookshelf.js. I assume though that pg is best for this.

One alternative I can think of is to read the full file, split it by semicolons, replace newlines with spaces, trim any duplicate space, then feed it into pg one by one in a manner that they’re executed sequentially, not asynchronously. I’m a little surprised if this is truly the most efficient way and also if no libraries exist yet to solve this. I’m a little hesitant to jump into it seeing as SQL syntax can itself be a little challenging and I might accidentally mash it up.

Some clarifications in advance:

  • psql cannot be used as it’s not installed on the target machine
  • I’ve chosen to develop and source control sql statements in sql native form, because it’s a lot easier for a DBA to use and manipulate it

Advertisement

Answer

I’ve written the following function which works for my case. It would have been much more simpler if it weren’t for:

  • Using batch to manage concurrency
  • Having the tricky PostgreSQL COPY case to consider

Code snippet:

Beware that this would fail if you used semicolons anywhere except to terminate SQL statements.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement