/supabase-tutorials

How to write stored procedures in Supabase?

Discover how to write, run, and modify SQL stored procedures in Supabase with our step-by-step guide for efficient database management.

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 write stored procedures in Supabase?

 

Step 1: Understanding Supabase and Stored Procedures

 

Before you begin writing stored procedures in Supabase, it's important to understand what Supabase is and what stored procedures do. Supabase is an open-source alternative to Firebase, providing a suite of tools for developing applications. In Supabase, stored procedures are written using SQL and are used to include logic that can be reused by various database requests.

 

Step 2: Set Up Your Supabase Project

 

Make sure you have a Supabase project set up. If you haven't already, you can set one up by creating an account on the Supabase website and starting a new project. After that, you will be provided with the credentials and a URL to use with your client.

 

Step 3: Accessing the SQL Editor

 

Go to your Supabase project dashboard and click on the "Database" option on the left-hand menu. Here you will find the SQL editor, which allows you to run SQL commands directly against your database. This is where you will write your stored procedures.

 

Step 4: Writing Your First Stored Procedure

 

To write a stored procedure, you must use the CREATE FUNCTION SQL command. Let's write a simple stored procedure to fetch data from a table called users.

<pre><code class="hljs">
CREATE FUNCTION get_all_users()
RETURNS TABLE(id INT, name TEXT, email TEXT) AS $$
BEGIN
    RETURN QUERY
    SELECT id, name, email FROM users;
END;
$$ LANGUAGE plpgsql;
</code></pre>

Here’s the breakdown of the stored procedure:

  • CREATE FUNCTION: This command creates a stored procedure named get_all_users.
  • RETURNS TABLE: Defines the types of data that the procedure returns. In this case, it's a table with id, name, and email.
  • LANGUAGE plpgsql: The procedure uses the PostgreSQL procedural language (PL/pgSQL).

 

Step 5: Running Your Stored Procedure

 

To run the stored procedure you created, you can call it like a regular SQL function:

<pre><code class="hljs">
SELECT * FROM get_all_users();
</code></pre>

Run this command in the SQL editor to execute the stored procedure and retrieve all users from the users table.

 

Step 6: Modifying a Stored Procedure

 

Suppose you need to modify the stored procedure to include a parameter for filtering users by a specific condition. Here’s how you could update the procedure:

<pre><code class="hljs">
CREATE OR REPLACE FUNCTION get_users_by_status(status TEXT)
RETURNS TABLE(id INT, name TEXT, email TEXT) AS $$
BEGIN
    RETURN QUERY
    SELECT id, name, email FROM users WHERE user_status = status;
END;
$$ LANGUAGE plpgsql;
</code></pre>
  • CREATE OR REPLACE FUNCTION: This command modifies an existing procedure or creates it if it doesn't exist.
  • Parameter status TEXT: This allows the function to accept a status parameter for filtering results.

 

Step 7: Testing Your Modified Stored Procedure

 

Test the modified stored procedure by running:

<pre><code class="hljs">
SELECT * FROM get_users_by_status('active');
</code></pre>

This will return all users where the user_status matches 'active'.

 

Step 8: Additional Enhancements

 

You can further enhance your stored procedures to handle more complex logic or integrate with other functions available in Supabase. Ensure your procedures are optimized for performance and security by consulting PostgreSQL documentation and best practices.

 

Step 9: Saving and Managing Stored Procedures

 

All stored procedures you create will be saved in your database system. You can view and manage them through the Supabase dashboard or using SQL commands within the SQL editor.

 

This step-by-step guide should help you get started with writing stored procedures in Supabase, enabling you to manage database logic effectively within your applications.

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