Array Function in PostgreSQL

Introduction

This tutorial will explain how to use the array function with the PostgreSQL database and the various necessary array functions. An array has an essential role in a PostgreSQL database and each type of data has its own corresponding array. For example, a character will have character[] array, an integer will have an integer[] array, and so on. PostgreSQL will create a corresponding array type in the background even if a user defines a data type. PostgreSQL also allows for defining a column of any valid data type for use as an array.

Prerequisites for using the array function in PostgreSQL

  • The PostgreSQL server must be properly installed, configured and running.

Download PostgreSQL for Linux and Windows systems here

The ARRAY_APPEND( ) Function in PostgreSQL

The ARRAY_APPEND() function adds an element to the end of a particular array. Following is the basic form of the ARRAY_APPEND() function:

array_append (the_array, the_element);

The steps for using the ARRAY_APPEND( ) function are as follows:

    1. Specify the ARRAY_APPEND( ) function.
    1. Specify the array to add the element to.
    1. Indicate the element that is to be added into the array.

How to use the ARRAY_APPEND( ) Function in PostgreSQL

This section will explain how to use the ARRAY_APPEND( ) function to add an additional element to an already existing set of elements within an array. For example, there may be an array of programming language such as ['php','go','ruby'] and the user wants to add the ‘python’ element into the array. This can be done by executing the following command:

SELECT ARRAY_APPEND(ARRAY['php','go','ruby'], 'python');

The results should resemble the following:

array_append
----------------------
{php,GO,ruby,python}
(1 ROW)

As shown above, the element ‘python’ was added at the end of the array.

How to Concatenate Strings with the ARRAY_CAT( ) Function

The previous section covered how to add an element at the end of an array using the ARRAY_APPEND( ) function. This section will explain how to concatenate strings using the ARRAY_CAT( ) function.

Following is the basic form of the ARRAY_CAT() function:

ARRAY_CAT(array_1, array_2);

The steps for using the ARRAY_CAT( ) function are as follows:

    1. Specify the ARRAY_CAT( ) function.
    1. Specify the arrays to concatenate.

How to use the ARRAY_CAT() function in PostgreSQL

This section will explain how to use the ARRAY_CAT( ) function. The following example shows an array of numbers that must be combined to create a collection of numbers:

SELECT ARRAY_CAT(ARRAY[63,41,55], ARRAY[90,56]);

The results should resemble the following:

array_cat
------------------
{63,41,55,90,56}
(1 ROW)

How to use the ARRAY_REMOVE( ) Function to Remove All of the Values in PostgreSQL

The ARRAY_REMOVE( ) function will remove all of the values that match the reference value from a set of elements within an array. Following is an example of the basic syntax of the ARRAY_REMOVE( ) function used to remove unneeded elements within the array:

array_remove(the_array, the_element);

The steps for the above ARRAY_REMOVE( ) function are as follows:

    1. Specify the ARRAY_REMOVE( ) function.
    1. Specify the array to will be removed from the element.
    1. Indicate what element will be removed within the specified array.

How to use the ARRAYREMOVE( ) function to Remove a _Specific Element in PostgreSQL

This section we will cover how to use the ARRAY_REMOVE( ) function to remove a specified element within an array. For example, say there is an array of teams and one of the teams repeatedly failed to show up for practice. The team is still showing up in the array, as [1,2,2,3,4,5,2]. Here team 2 will be removed from the array with the following command:

SELECT array_remove(ARRAY[1,2,2,3,4,5,2],2);

The results should resemble the following:

array_remove
--------------
{1,3,4,5}
(1 ROW)

As shown above, team 2 was removed from the array.

How to use the ARRAY_REPLACE( ) Function in PostgreSQL

The ARRAY_REPLACE( ) function in PostgreSQL will replace an array element with a new value that matches the specified criteria.

Following is the basic form of the ARRAY_REPLACE( ) function:

array_replace(the_array, element_to_replace, new_value);

The steps used in the above ARRAY_REPLACE( ) function are as follows:

    1. Specify the ARRAY_REPLACE( ) function.
    1. Identify the element being replaced with a new value.
    1. Specify the new value of the element.

How to use the ARRAY_REPLACE( ) function in PostgreSQL

This section will cover how to use the ARRAY_REPLACE( ) function to update or replace the array elements with a new value. The following statement is an example of a developer who wants to update a skill set:

SELECT array_replace(ARRAY['css','python','go','nodeJs','java'], 'css', 'php');

The results should resemble the following:

array_replace
-----------------------------
{php,python,GO,nodeJs,java}
(1 ROW)

The above image shows that ‘css’ was successfully replaced by ‘php’.

Conclusion

This tutorial explained how to use the array function in PostgreSQL. The article specifically covered how the ARRAY_APPEND( ) function, the Concatenate Strings with the ARRAY_CAT( ) function, the ARRAY_REMOVE( ) and the ARRAY_REPLACE( ) function in PostgreSQL. The tutorial provided working examples and a detailed explanation of each function. Remember that each array has an essential role in the PostgreSQL database and each data type has its own corresponding array.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.