apps/docs/content/guides/database/arrays.mdx
Postgres supports flexible array types. These arrays are also supported in the Supabase Dashboard and in the JavaScript API.
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">
arraytest.textarray, type text, and select Define as array.create table arraytest (
id integer not null,
textarray text array
);
<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"
<TabPanel id="dashboard" label="Dashboard">
arraytest table.["Harry", "Larry", "Moe"].INSERT INTO arraytest (id, textarray) VALUES (1, ARRAY['Harry', 'Larry', 'Moe']);
Insert a record from the JavaScript client:
const { data, error } = await supabase
.from('arraytest')
.insert([{ id: 2, textarray: ['one', 'two', 'three', 'four'] }])
Insert a record from the Swift client:
struct ArrayTest: Encodable {
let id: Int
let textarray: [String]
}
try await supabase
.from("arraytest")
.insert(
[
ArrayTest(
id: 2,
textarray: ["one", "two", "three", "four"]
)
]
)
.execute()
Insert a record from the Python client:
supabase.from_('arraytest').insert(
[
{
id: 2,
textarray: ["one", "two", "three", "four"]
}
]
)
.execute()
<Tabs scrollable size="small" type="underlined" defaultActiveId="dashboard" queryGroup="database-method"
<TabPanel id="dashboard" label="Dashboard">
arraytest table.You should see:
| id | textarray |
| --- | ----------------------- |
| 1 | ["Harry","Larry","Moe"] |
select * from arraytest;
You should see:
| id | textarray |
| --- | ----------------------- |
| 1 | ["Harry","Larry","Moe"] |
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:
SELECT textarray[1], array_length(textarray, 1) FROM arraytest;
returns:
| textarray | array_length |
| --------- | ------------ |
| Harry | 3 |
This returns the entire array field:
const { data, error } = await supabase.from('arraytest').select('textarray')
console.log(JSON.stringify(data, null, 2))
returns:
[
{
"textarray": ["Harry", "Larry", "Moe"]
}
]
This returns the entire array field:
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"],
)
]