Supabase SQL

Supabase SQL

Star on Github

Add Constraint

Template for adding a constraint.

Add column

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

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.

Case Expression

Case Expression.

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.

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.

Drop View

Template for dropping / removing a view.

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.

Get columns info of a table

Template to describe a table.

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.

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).

Query specific text from text[]

Query rows that has specific text you need in a text array.

RLS Advanced policies

Row level security with advanced/complicated policies.

RLS Edit Policy

Row level security editing.

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.

Seed unlimited users

SQL seed function for table auth.users.

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.

World Countries

Create a Country table for every countries in the world.

Sequence operations

Define a sequence generator.

Create Sequence

Syntax to create a sequence generator in Postgres SQL.

create [ temporary | temp ] sequence [ if not exists ] sequence_name [ increment [ by ] increment ]
    [ minvalue min_value | no minvalue ] [ maxvalue max_value | no maxvalue ]
    [ start [ with ] start_value ] [ cache cache ] [ [ no ] cycle ]
    [ owned by { table_name.column_name | none } ]

-- Example 1. Create a sequence starting from 1 and incremented by 1 with no maximum and minimum value
create sequence test_seq increment by 1 start with 1 no maxvalue no minvalue; -- test_seq is the sequence name

-- Example 2. Create a sequence starting from 100 and incremented by 10
create sequence test_seq increment by 10 start with 100; -- test_seq is the sequence name

Next Value in Sequence

Fetch the next value of the sequence created.

select nextval('sequence_name');
-- Example
select nextval('test_seq'); -- test_seq is the sequence name

Last Value or current value of the sequence

Query to fetch most recently obtained value from a sequence

select currval('sequence_name');
-- Example
select currval('test_seq') -- test_seq is the sequence name

select last_value from sequence_name;
-- Example
select last_value from test_sequence; -- test_seq is the sequence name

Set Value of the sequence

Set the current value of the sequence to specific number within the range as defined at time of sequence generation.

select setval('sequence_number',value);
-- Example
select setval('test_seq',5)

Drop Sequence

Drop the sequence.

drop sequence sequence_name;
-- Example
drop sequence test_seq; -- test_seq is the sequence name
Edit this script Updated at Sun, Jun 4, 2023