Skip to main content

Working With Arrays

PostgreSQL supports flexible array types.
These arrays are also supported in the Supabase dashboard and in the Javascript API.

Steps#

Create a table with an array column#

Create a test table with a text array (an array of strings):

1. Got the Table Editor
2. Create a New Table
3. Create a new table named `arraytest` and save the table
- New Column
- Name the new column `textarray`
- Make it type `text`
- Check the box labelled `Define as array`
- Save the new column

Insert a record with an array value#

- Table Editor
- Select `arraytest`
- Insert Row
- In the `textarray` field input, paste:
- `["Harry", "Larry", "Moe"]`
- Save the new row

View the results#

- Table Editor
- Select `arraytest`

Your first array data!

idtextarray
1["Harry","Larry","Moe"]

Query Array Data#

To query an array, PostgreSQL uses 1-based arrays, so be careful, since you're probably used to 0-based arrays in Javascript.

To select the first item from the array and get the total length of the array:

SELECT textarray[1], array_length(textarray, 1) FROM arraytest;

returns:

textarrayarray_length
Harry3

Resources#