Adding value to Postgres integer array
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 array
arrayName int8[];
-- Adding value 2206 to int array
arrayName := arrayName || 2206;
-- looping throught the array
FOREACH i IN ARRAY arrayName
LOOP
RAISE NOTICE 'array value %', i;
END LOOP;
cheers
Use array_append
function to append an element at the end of an array:
UPDATE table1
SET 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}