/supabase-tutorials

How to use triggers in Supabase?

Learn how to set up and test PostgreSQL triggers in Supabase. This guide walks you through creating projects, tables, trigger functions, and automating logs.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use triggers in Supabase?

 

Step 1: Set Up a Supabase Project

 

Begin by setting up a Supabase project. If you don't have an account, sign up at Supabase. Once logged in, create a new project by following these steps:

  • Click on the "New Project" button.
  • Fill in the details, such as project name and database password.
  • Choose a region that is geographically close to you.
  • Click "Create Project."

This will set up a new Supabase project with its own dedicated PostgreSQL database.

 

Step 2: Access the Supabase Dashboard

 

Once the project is ready, you will be redirected to the Supabase dashboard:

  • Navigate to the "Database" section in the menu.
  • Select "SQL Editor" to execute SQL queries.

 

Step 3: Create a Table

 

To work with triggers, you'll first need a table. Create a new table using SQL commands:


CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE
);
  • Paste the SQL in the SQL Editor and run it to create the table.

 

Step 4: Create a Trigger Function

 

Create a trigger function that will execute a specific action automatically:


CREATE FUNCTION log_user_insertion() 
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO user_logs (user_id, action, created\_at)
  VALUES (NEW.id, 'insert', NOW());
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
  • This function logs every insertion into a separate user_logs table.

 

Step 5: Create the User Logs Table

 

Ensure you have a table to store trigger logs:


CREATE TABLE user\_logs (
  id SERIAL PRIMARY KEY,
  user\_id INTEGER NOT NULL,
  action TEXT NOT NULL,
  created\_at TIMESTAMP NOT NULL
);
  • Execute this SQL to create the user_logs table.

 

Step 6: Attach the Trigger to the Users Table

 

Now, attach the trigger function to the users table:


CREATE TRIGGER after_user_insert
AFTER INSERT ON users
FOR EACH ROW EXECUTE FUNCTION log_user_insertion();
  • Running this SQL will ensure that the log_user_insertion function is executed after every row insertion into the users table.

 

Step 7: Test the Trigger

 

Insert a new record into the users table to test if the trigger works:


INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
  • Check the user_logs table to see if the log has been created:

SELECT \* FROM user\_logs;
  • Verify that the log entry corresponding to the insertion into users is recorded.

 

Step 8: Manage and Modify Triggers

 

To manage or modify triggers, navigate to the SQL Editor:

  • Update the trigger function by redefining it.
  • Drop a trigger if you no longer need it:

DROP TRIGGER IF EXISTS after_user_insert ON users;
  • Adjust the trigger function logic for more complex scenarios as needed.

 

Conclusion

 

You've successfully set up and tested a PostgreSQL trigger within your Supabase project. Use this approach to automate database operations and maintain data consistency across your project.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022