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:
1 | array_append (the_array, the_element); |
The steps for using the ARRAY_APPEND( ) function are as follows:
- Specify the ARRAY_APPEND( ) function.
- Specify the array to add the element to.
- 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:
1 | SELECT ARRAY_APPEND(ARRAY['php','go','ruby'], 'python'); |
The results should resemble the following:
1 2 3 4 | 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:
1 | ARRAY_CAT(array_1, array_2); |
The steps for using the ARRAY_CAT( ) function are as follows:
- Specify the ARRAY_CAT( ) function.
- 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:
1 | SELECT ARRAY_CAT(ARRAY[63,41,55], ARRAY[90,56]); |
The results should resemble the following:
1 2 3 4 | 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:
1 | array_remove(the_array, the_element); |
The steps for the above ARRAY_REMOVE( ) function are as follows:
- Specify the ARRAY_REMOVE( ) function.
- Specify the array to will be removed from the element.
- 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:
1 | SELECT array_remove(ARRAY[1,2,2,3,4,5,2],2); |
The results should resemble the following:
1 2 3 4 | 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:
1 | array_replace(the_array, element_to_replace, new_value); |
The steps used in the above ARRAY_REPLACE( ) function are as follows:
- Specify the ARRAY_REPLACE( ) function.
- Identify the element being replaced with a new value.
- 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:
1 | SELECT array_replace(ARRAY['css','python','go','nodeJs','java'], 'css', 'php'); |
The results should resemble the following:
1 2 3 4 | 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