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.
Schedule PostgreSQL commands directly from the database.
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 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.
Delete items in Storage using SQL
Template for dropping / removing constraint.
Template for dropping / removing function.
Template for dropping / removing row level security.
Template for dropping / removing trigger from table.
How to use full text search in PostgreSQL.
Generate YouTube-like short IDs as Postgres Primary Keys.
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).
Row level security with advanced/complicated policies.
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.
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.
Generate YouTube-like short IDs as Postgres Primary Keys.
This installs a trigger and type which allow you to use YouTube-like short IDs (e.g. 1TNhBqYo-6Q) as Postgres Primary Keys. Just like the YouTube IDs, SHORTKEY IDs are fixed length and URL-safe.
create table test (id shortkey primary key, name text);
create trigger trigger_test_genid before insert on test for each row execute procedure shortkey_generate();
-- generate
insert into test(name) values ('bob'), ('joe');
-- user-supplied id
insert into test(id, name) values ('1tnhbqyo-6q', 'lisa');
select * from test;
-- id name
-- 4e_z0mhjvrk bob
-- wiz_j0hibuq joe
-- 1tnhbqyo-6q lisa
shortkey
is compatible with text
and should be handled as such by most sane ORMs. If not, create a type mapping to whatever your string type is in your app/ORM.
SHORTKEYs are truly random, so they fragment the index space just like v4 UUIDs, decreasing performance slightly. But unlike UUID (which provides an instance-local pseudo-sequential type via UUID v1 MC), that behavior can't be changed. This is intentional. SHORTKEYs are supposed to be used for external facing IDs, like in APIs.
There, they prevent enumeration and cardinality evaluation (e.g. how many things are there and what's the next/previous thing - just like YouTube). Use where appropriate.