resthttpsupabasepostgrest

Supabase: get table count in rest mode


There is any way to get the table count in rest mode?

I'm using query params like:

https://urt.to.supabase/rest/v1/table?select=field,another_field

I was googling it out but without success.


Solution

  • What you need to do is to add Prefer: count=exact to the request header.

    Example of this using curl would be like this:

    curl "https://urt.to.supabase/rest/v1/table?select=field,another_field" -I \
      -H "Prefer: count=exact"
    

    The count value will be the number after / on the content-range header on your response. In the following example, row count is 3573458.

    Content-Range: 0-24/3573458
    

    You can read more about it on postgrest documentation here.

    Note that with the Supabase.js library, you easily get the count of

    const { data, error, count } = await supabase
      .from('cities')
      .select('name', { count: 'exact' })
    

    You can read more about it on the official docs.