3

I am trying it insert a simple array of numbers as different rows of a PostgreSQL table.

For example, I want to insert:

const arr = [1.1, 2.0, 3.40, 4.5, -5];

so that table 'numeric_values_list' populates to:

id | val 
--------
1  | 1.1
2  | 2.0
3  | 3.40
4  | 4.5
5  | -5.6

Using the psql terminal, I can get this query to produce the desired result:

INSERT INTO numeric_values_list(val)
SELECT * FROM UNNEST (ARRAY[1.1, 2.0, 3.40, 4.5, -5]);

However, I cannot figure out the proper syntax for the postgres-pg query string that will produce the same result. The following has not worked for me:

  const list = [1.1, 2.0, 3.40, 4.5, -5];

  // version 1
  db.query('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST($1)', list)

  // version 2
  db.query('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST($1::numeric[])', list)

  // version 3
  db.query('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST($1::[])', list)

  // version 4
  db.query('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST ARRAY[($1)]', list)

My question is how do I get this to work using the pg-promise library?
Thanks much.

2
  • 1
    With pg-promise you could simply use csv filter, and it would work ;) Commented Sep 4, 2019 at 3:40
  • 1
    oh, that is beautiful. If you want to move your comment to an answer, I will check it as correct. Commented Sep 4, 2019 at 20:45

2 Answers 2

1

This can be much simpler when using pg-promise:

const arr = [1.1, 2.0, 3.40, 4.5, -5];

db.none('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST(ARRAY[$1:csv])', [arr])
  .catch(error => {
      /* when in error */
  });

See CSV Filter.

And if your input is always a non-empty array, then it can be further simplified to just this:

db.none('INSERT INTO numeric_values_list(val) SELECT * FROM UNNEST($1)', [arr])
  .catch(error => {
      /* when in error */
  });

That's because by default a non-empty array is formatted by pg-promise as array[...].

Check this out:

const pgp = require('pg-promise')(/* init options */);

const s = pgp.as.format('$1', [arr]);

console.log(s); /*=> array[1.1,2,3.4,4.5,-5] */
Sign up to request clarification or add additional context in comments.

Comments

0

UPDATE:

Using vitaly-t's suggestion, the following code does exactly what I wanted it to do:

const arr = [1.1, 2.0, 3.40, 4.5, -5];

...

db.query('                             \
  INSERT INTO numeric_values_list(val) \
  SELECT * FROM UNNEST(ARRAY[$1:csv])  \
', arr]);

ORIGINAL POST:

Not sure if this solution is ideal, but the following query produced the desired result:

const arr = [1.1, 2.0, 3.40, 4.5, -5];

const strList = String(arr);
const delimiter = ',';

...

db.query('                                                 \
  INSERT INTO numeric_values_list(val)                     \
  SELECT * FROM UNNEST(STRING_TO_ARRAY($1, $2)::numeric[]) \
', [strList, delimiter]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.