PostgreSQL array_append() Function

The PostgreSQL array_append() function appends the specified element to the end of the specified array and returns the modified array.

array_append() Syntax

Here is the syntax of the PostgreSQL array_append() function:

array_append(array, element) -> array

Parameters

array

Required. The array to append elements to.

element

Required. The element to append to the array.

Return value

The PostgreSQL array_append() function returns an array with the specified element appended at the end.

If the argument array is NULL, the array_append() function will return an array including one element element.

The type of the element appended needs to be the same as the type in the array, otherwise the array_append() function will give an error message.

array_append() Examples

This example shows how to use the PostgreSQL array_append() function to append an element 3 to {0,1,2}.

SELECT array_append(ARRAY[0, 1, 2], 3);
 array_append
--------------
 {0,1,2,3}

You can provide a null value for the parameter array, for example:

SELECT array_append(NULL, 1);
 array_append
--------------
 {1}

Here we append 1 to an null array, and get an array including only 1.

You cannot add elements of different data types to an array. For example, you cannot add an element of type string to an integer array like this:

SELECT array_append(ARRAY[0, 1, 2], 'three');

The array_append() function will return an error.