How to Create an Array

In PostgreSQL we can create an array by using the reserved word ARRAY or by casting as we can see in the next example:

select ARRAY[5,4,1,0] AS using_ARRAY, '{5,4,1,0}'::integer[] AS using_cast
text_array using_cast
{5,4,1,0} {5,4,1,0}

Array can have elements of different data types, however all the elements in the array must be of the same data type. Let’s see an array of text values and an array of dates

select	ARRAY[‘apple’,’banana’,‘orange’] AS text_array, 
  	ARRAY[‘3/6/2021’,’4/4/2021’] AS date_array
text_array date_array
{apple,banana,orange} {3/6/2021,4/4/2021}

IN THIS PAGE