Arrays

An array is a data structure that consists of a collection of elements (values or variables); the order of the elements in the array significant and each element in the array is identified by an index. The index of the array is often starts at 1, but in some programming languages, such as C and C++, the index starts at 0. Array elements should have the same type, such as INT or TEXT.

Multidimensional arrays are supported; the array type can be a base, enum, or composite type. Array elements should have only one data type. PostgreSQL arrays allow duplicate values as well as null values. The following example shows how to initialize a one-dimensional array and get the first element:

car_portal=> SELECT ('{red, green, blue}'::text[])[1] ...

Get Learning PostgreSQL 11 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.