question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to insert multiple rows with a single query?

See original GitHub issue

I have n number of items. I would like to add them all with a single insert query.

insert into testtable(id,name) values (1,"jack"),(2,"john"),(3,"jill");

I have an array for my rows, and I like to add them all in. So, if I provide pg the correct string (after manually forming it in a variable) it works perfectly fine, but this all seemed a bit pointless to me. I expect pg to handle arrays for me as well, but I’m unable to find the option for that. So I’m asking, is this possible?

How can I insert multiple rows from an array of rows, with a single insert query with this library?

Would love to know. Thank you.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:21
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

200reactions
rpedelacommented, Mar 22, 2016

Using pg-format with a nested array:

var format = require('pg-format');

var values = [
    [ 1, 'jack' ],
    [ 2, 'john' ],
    [ 3, 'jill' ],
];
console.log(format('INSERT INTO test_table (id, name) VALUES %L', values));
// INSERT INTO test_table (id, name) VALUES ('1', 'jack'), ('2', 'john'), ('3', 'jill')

Disclaimer: I wrote pg-format.

113reactions
charmandercommented, Apr 20, 2017

@x0days That’s for MySQL, not PostgreSQL.

@zettam:

pg.query(
    "INSERT INTO testtable (id, name) SELECT * FROM UNNEST ($1::int[], $2::text[])",
    [
        [1, 2, 3],
        ["Jack", "John", "Jill"],
    ]
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server INSERT Multiple Rows Into a Table Using One ...
To insert multiple rows returned from a SELECT statement, you use the INSERT INTO SELECT statement. SQL Server INSERT multiple rows – examples....
Read more >
Inserting multiple rows in a single SQL query? - Stack Overflow
INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists ...
Read more >
SQL Query to Insert Multiple Rows - GeeksforGeeks
In this article, we see how to insert individual as well as multiple rows in a database using the INSERT statement in the...
Read more >
How to INSERT Multiple Records in SQL - DigitalOcean
SQL INSERT query inserts data into the columns of a particular table. The normal SQL INSERT query inputs the data values in a...
Read more >
SQL INSERT Multiple Rows - Javatpoint
SQL INSERT Multiple Rows · mysql> USE dbs; · mysql> CREATE TABLE student(ID INT, Name VARCHAR(20), Percentage INT, Location VARCHAR(20), DateOfBirth DATE);.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found