How to Insert Data Into an Array

If you want to add elements to an existing array you can use the function array_append or the concatenation operator. Let’s see some examples:

create table array_example ( fruits text[] );
insert into array_example values ( ARRAY[‘apple’,’banana’,‘orange’] );
 
select fruits from array_example;
fruits text[]
{apple, banana, orange}

Let’s add a new element (‘peach’) by using the array_append function

update array_example set fruits = array_append(fruits,'peach');
 
select fruits from array_example;
fruits text[]
{apple, banana, orange, peach}

Let’s add a new element (‘grape’) by using the concatenation operator.

update array_example set fruits = ARRAY['grape' ] || fruits;
 
select fruits from array_example;
fruits text[]
{grape, apple, banana, orange, peach}

IN THIS PAGE