Adding value to Postgres integer array Adding value to Postgres integer array postgresql postgresql

Adding value to Postgres integer array


Use array_append function to append an element at the end of an array:

UPDATE table1SET integer_array = array_append(integer_array, 5);

5 is a value of choice, it's of an integer datatype in your case. You probably need some WHERE clause as well not to update the entire table.

Try below to see how it works:

SELECT ARRAY[1,2], array_append(ARRAY[1,2],3);

Result:

 array | array_append-------+-------------- {1,2} | {1,2,3}


I like this way better:

UPDATE table1 SET integer_array = integer_array || '{10}';

You can also add multiple values with single query:

UPDATE table1 SET integer_array = integer_array || '{10, 11, 12}';


-- Declaring the arrayarrayName int8[];-- Adding value 2206 to int arrayarrayName := arrayName || 2206;-- looping throught the arrayFOREACH i IN ARRAY arrayName LOOP  RAISE NOTICE 'array value %', i;END LOOP;

cheers