PostgreSQL 18 reference · Array Functions

PostgreSQL ARRAY_POSITION() Function

Return the 1-based position of the first occurrence of a value in an array, or NULL if it isn't there.

PostgreSQL 18 Since 9.5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_position(array, element, start)  |  array_positions(array, element)

Parameters

ParameterDescription
array An array value.
element The value to find.
start optional Where to start searching.

Return value

integer, or NULL.

Examples

Find a value
SELECT array_position(ARRAY['mon', 'tue', 'wed'], 'tue') AS pos,
       array_position(ARRAY['mon'], 'sun') AS missing;

Output

 pos | missing
-----+---------
   2 |    NULL
(1 row)

Run this on your computer — see “Get Started” for setup.

Every position
SELECT array_positions(ARRAY[1, 2, 1, 3, 1], 1) AS all_positions;

Output

 all_positions
---------------
 {1,3,5}
(1 row)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.5Added

Was this page helpful?