Supabase is an open-source alternative to Firebase that provides a suite of tools to help developers build scalable applications quickly and easily. In this tutorial, we will explore how to use Supabase with Python to create a simple CRUD (create, read, update, delete) application.
Step 1: Setting up Supabase
-
Sign up for a free Supabase account at https://app.supabase.io/. Once you have created an account, you will be given a URL and API key that you will need to connect to your Supabase project.
-
Create a new Supabase project and database by clicking on the "New Project" button. Choose a name for your project and select a region for your database.
- Once your project is created, click on the "Access Settings" tab to find your URL and API key. Copy both of these values, as you will need them to connect to your database from your Python application.
Step 2: Setting up Python
-
Open your terminal and create a new directory for your Python project. Navigate to this directory using the
cd
command. - Create a new Python virtual environment by running the following command:
python3 -m venv venv
- Activate the virtual environment by running the following command:
source venv/bin/activate
- Install the
supabase-py
library by running the following command:
pip install supabase-py
Step 3: Connecting to Supabase
- Create a new Python script in your project directory and import the
supabase
library:
from supabase_py import create_client
- Initialize a Supabase client object using your Supabase URL and API key:
supabase_url = 'YOUR_SUPABASE_URL'
supabase_key = 'YOUR_SUPABASE_KEY'
client = create_client(supabase_url, supabase_key)
Step 4: Performing CRUD operations
Now that you have connected to your Supabase project, you can start performing CRUD operations on your database. Below are examples of how to create, read, update, and delete records in your database using the supabase
library.
- Creating a new record:
data = {'name': 'John Doe', 'email': 'john.doe@example.com'}
client.table('users').insert(data)
- Reading records from the database:
result = client.table('users').select('*').execute()
for row in result['data']:
print(row)
- Updating a record:
data = {'email': 'john.doe@example.org'}
client.table('users').update(data).where('name', 'John Doe').execute()
- Deleting a record:
client.table('users').delete().where('name', 'John Doe').execute()
Step 5: Conclusion
In this tutorial, you have learned how to set up Supabase, connect to your database using Python, and perform CRUD operations on your database using the supabase
library. With Supabase and Python, you can quickly build scalable applications that are easy to maintain and manage. Feel free to explore the Supabase documentation for more advanced features and capabilities. Happy coding!
hi, i get a APIError code 42501 , message permission denied for schema public. What i can do for that ?
very good tutorial
Thank you
bad video, little content
Thank you very much for this!