Supabase SQL

Supabase SQL

Star on Github

Add Constraint

Template for adding a constraint.

Automatically update timestamps

Update a column timestamp on every update.

Basic Aggregrate Functions

Set of functions to perform calculation on a set of values. The return is a single summary value. (Except for "round()")

CRON job with pg_cron

Schedule PostgreSQL commands directly from the database.

Change Column Type

Template to modify a column type. Make sure to change the name and type.

Create View

Basic view template. Change according to your preference.

Create basic table

Basic table template. Change "table_name" to the name you prefer.

Create function

Template to create a simple function.

Create function that return integer

Template to create a function that return integer like count of rows.

Create function that return table

Template to create a function a function that return set of table.

Create table with constraints

Table with constraints example.

Create table with foreign key

Table with foreign key (fk) template. Change "table_name" to the name you prefer.

Delete Storage Item

Delete items in Storage using SQL

Drop Constraint

Template for dropping / removing constraint.

Drop Function

Template for dropping / removing function.

Drop RLS

Template for dropping / removing row level security.

Drop Trigger

Template for dropping / removing trigger from table.

Full Text Search

How to use full text search in PostgreSQL.

Generate Youtube-like short ID

Generate YouTube-like short IDs as Postgres Primary Keys.

Handle New User - Function & Trigger

Insert new user data into another table when event new user signup using Supabase Auth.

Increment field value

Update a field with incrementing value using stored procedure.

Javascript

Javascript snippets.

List all constraints

List all constraints and their tables.

List all foreign keys

List all foreign keys (FKs) and their columns.

List all functions

List all the functions in (predefined/user-defined).

List all primary keys

List all primary keys (PKs) and their columns.

List all table size

List all table data size.

List all triggers

List all the triggers in (predefined/user-defined).

RLS Advanced policies

Row level security with advanced/complicated policies.

RLS Policies with joins

Row level security that requires table joins .

RLS Policies with security definer functions

Row level security that make use of security definer functions .

RLS Read Access

Row level security for read access.

RLS Restrict updates

Row level security for restrict updates.

RLS Time to live for rows

Row level security policies to implement TTL.

RLS Verifying email domains

Row level security that verify email domains.

Rename Constraints

Template to rename any constraints you have.

Sequence operations

Define a sequence generator.

Show Postgres version

Check your database version.

Stripe subscriptions

Starter template for the Next.js Stripe Subscriptions Starter.

Todo List

Build a todo list with Row Level Security.

Update Constraint

Template for updating a constraint.

List all constraints

List all constraints and their tables.

select tc.table_schema,
  tc.table_name,
  string_agg(col.column_name, ', ') as columns,
  tc.constraint_name,
  cc.check_clause
from information_schema.table_constraints tc
join information_schema.check_constraints cc
  on tc.constraint_schema = cc.constraint_schema
  and tc.constraint_name = cc.constraint_name
join pg_namespace nsp on nsp.nspname = cc.constraint_schema
join pg_constraint pgc on pgc.conname = cc.constraint_name
  and pgc.connamespace = nsp.oid
  and pgc.contype = 'c'
join information_schema.columns col
  on col.table_schema = tc.table_schema
  and col.table_name = tc.table_name
  and col.ordinal_position = ANY(pgc.conkey)
where tc.constraint_schema not in('pg_catalog', 'information_schema')
group by tc.table_schema,
  tc.table_name,
  tc.constraint_name,
  cc.check_clause
order by tc.table_schema,
  tc.table_name;
Edit this script Updated at Wed, Jan 18, 2023