Template for adding a constraint.
Template to add a column. Make sure to change the name and type.
Update a column timestamp on every update.
Set of functions to perform calculation on a set of values. The return is a single summary value. (Except for "round()")
Schedule PostgreSQL commands directly from the database.
Case Expression.
Template to modify a column type. Make sure to change the name and type.
Basic view template. Change according to your preference.
Basic table template. Change "table_name" to the name you prefer.
Template to create a simple function.
Template to create a function that return integer like count of rows.
Template to create a function a function that return set of table.
Table with constraints example.
Table with foreign key (fk) template. Change "table_name" to the name you prefer.
Template for dropping / removing constraint.
Template for dropping / removing function.
Template for dropping / removing row level security.
Template for dropping / removing trigger from table.
Template for dropping / removing a view.
How to use full text search in PostgreSQL.
Generate YouTube-like short IDs as Postgres Primary Keys.
Template to describe a table.
Insert new user data into another table when event new user signup using Supabase Auth.
Update a field with incrementing value using stored procedure.
List all constraints and their tables.
List all foreign keys (FKs) and their columns.
List all the functions in (predefined/user-defined).
List all primary keys (PKs) and their columns.
List all table data size.
List all the triggers in (predefined/user-defined).
Query rows that has specific text you need in a text array.
Row level security with advanced/complicated policies.
Row level security editing.
Row level security that requires table joins .
Row level security that make use of security definer functions .
Row level security for read access.
Row level security for restrict updates.
Row level security policies to implement TTL.
Row level security that verify email domains.
Template to rename any constraints you have.
SQL seed function for table auth.users.
Define a sequence generator.
Check your database version.
Starter template for the Next.js Stripe Subscriptions Starter.
Build a todo list with Row Level Security.
Template for updating a constraint.
Create a Country table for every countries in the world.
Row level security with advanced/complicated policies.
profiles
in the public schema (default schema).create table posts (
id serial primary key,
creator_id uuid not null references auth.users(id),
title text not null,
body text not null,
publish_date date not null default now(),
audience uuid[] null -- many to many table omitted for brevity
);
create table comments (
id serial primary key,
post_id int not null references posts(id) on delete cascade,
user_id uuid not null references auth.users(id),
body text not null,
comment_date date not null default now()
);
create policy "Creator can see their own posts"
on posts
for select
using (
auth.uid() = posts.creator_id
);
create policy "Logged in users can see the posts if they belong to the post 'audience'."
on posts
for select
using (
auth.uid() = any (posts.audience)
);
create policy "Users can see all comments for posts they have access to."
on comments
for select
using (
exists (
select 1 from posts
where posts.id = comments.post_id
)
);