I'm building a nestjs application that uses postgres. I have some big data (from 100 to 10 millions entries and more) that I receive through an API. I need to write this data to a postgres DB.
I researched a bit and found out that I should use COPY to do a bulk insert.
I tested COPY in console mode using psql and it works.
But I can't find any example how to do it through js code.
Here is what I've tried:
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
//throws error 'syntax error at or near "123"'
const query = `COPY ${table}(key) FROM stdin \n123\t\n321\t\n147\t\n\\.\n`;
// using INSERT works as expected
//const query = `INSERT INTO ${table}(key) VALUES ('123'), ('321'), ('147');`;
await queryRunner.query(query);
await queryRunner.commitTransaction();
await queryRunner.release();
The above code throws this error:
ERROR: syntax error at or near "123" at character 33
Can anyone explain/point into direction how to solve this? Thanks