PostgreSQL array_prepend() Function

The PostgreSQL array_prepend() function prepends the specified element to the start of the specified array and returns the modified array.

array_prepend() Syntax

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

array_prepend(element, array) -> array



Required. The element to prepend to the array.


Required. The array where the new element prepended to.

Return value

The PostgreSQL array_prepend() function returns an array with the specified element prepended to its start.

If the argument array is NULL, the array_prepend() function will return an array only include one element element.

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

array_prepend() Examples

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

SELECT array_prepend(3, ARRAY[0, 1, 2]);

You can prepend an element to a null array, for example:

SELECT array_prepend(1, NULL);

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_prepend('three', ARRAY[0, 1, 2]);

The array_prepend() function returns an error.