Back to Supabase

Working With Arrays

apps/docs/content/guides/database/arrays.mdx

1.26.044.3 KB
Original Source

Postgres supports flexible array types. These arrays are also supported in the Supabase Dashboard and in the JavaScript API.

Create a table with an array column

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

<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"

<TabPanel id="dashboard" label="Dashboard">
  1. Go to the Table editor page in the Dashboard.
  2. Click New Table and create a table with the name arraytest.
  3. Click Save.
  4. Click New Column and create a column with the name textarray, type text, and select Define as array.
  5. Click Save.
</TabPanel> <TabPanel id="sql" label="SQL">
sql
create table arraytest (
  id integer not null,
  textarray text array
);
</TabPanel> </Tabs>

Insert a record with an array value

<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"

<TabPanel id="dashboard" label="Dashboard">
  1. Go to the Table editor page in the Dashboard.
  2. Select the arraytest table.
  3. Click Insert row and add ["Harry", "Larry", "Moe"].
  4. Click Save.
</TabPanel> <TabPanel id="sql" label="SQL">
sql
INSERT INTO arraytest (id, textarray) VALUES (1, ARRAY['Harry', 'Larry', 'Moe']);
</TabPanel> <TabPanel id="js" label="JavaScript">

Insert a record from the JavaScript client:

js
const { data, error } = await supabase
  .from('arraytest')
  .insert([{ id: 2, textarray: ['one', 'two', 'three', 'four'] }])
</TabPanel> <$Show if="sdk:swift"> <TabPanel id="swift" label="Swift">

Insert a record from the Swift client:

swift
struct ArrayTest: Encodable {
  let id: Int
  let textarray: [String]
}

try await supabase
  .from("arraytest")
  .insert(
    [
      ArrayTest(
        id: 2,
        textarray: ["one", "two", "three", "four"]
      )
    ]
  )
  .execute()
</TabPanel> </$Show> <$Show if="sdk:python"> <TabPanel id="python" label="Python">

Insert a record from the Python client:

python
supabase.from_('arraytest').insert(
  [
    {
      id: 2,
      textarray: ["one", "two", "three", "four"]
    }
  ]
)
.execute()
</TabPanel> </$Show> </Tabs>

View the results

<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"

<TabPanel id="dashboard" label="Dashboard">
  1. Go to the Table editor page in the Dashboard.
  2. Select the arraytest table.

You should see:

| id  | textarray               |
| --- | ----------------------- |
| 1   | ["Harry","Larry","Moe"] |
</TabPanel> <TabPanel id="sql" label="SQL">
sql
select * from arraytest;

You should see:

| id  | textarray               |
| --- | ----------------------- |
| 1   | ["Harry","Larry","Moe"] |
</TabPanel> </Tabs>

Query array data

Postgres uses 1-based indexing (e.g., textarray[1] is the first item in the array).

<Tabs scrollable size="small" type="underlined" defaultActiveId="sql" queryGroup="language"

<TabPanel id="sql" label="SQL">

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

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

returns:

| textarray | array_length |
| --------- | ------------ |
| Harry     | 3            |
</TabPanel> <TabPanel id="js" label="JavaScript">

This returns the entire array field:

js
const { data, error } = await supabase.from('arraytest').select('textarray')
console.log(JSON.stringify(data, null, 2))

returns:

json
[
  {
    "textarray": ["Harry", "Larry", "Moe"]
  }
]
</TabPanel> <$Show if="sdk:swift"> <TabPanel id="swift" label="Swift">

This returns the entire array field:

swift
struct Response: Decodable {
  let textarray: [String]
}

let response: [Response] = try await supabase.from("arraytest").select("textarray").execute().value
dump(response)

returns:

[
  Response(
    textarray: ["Harry", "Larry", "Moe"],
  )
]
</TabPanel> </$Show> </Tabs>

Resources