Skip to Content
PostgreSQL for AISupabase快速开始

1. 准备工作

1. 创建 Supabase 实例

参见创建 Supabase 实例

2. 获取 API 凭证

  1. 选中 Supabase 实例,点击”详情”按钮。
  2. 开启外网访问,并将客户端 IP 地址添加至白名单。
  3. 点击”前往查看”,进入 Supabase 登录页面。
  4. 输入默认用户名 Supabase 及对应密码,登录至 Supabase 项目。
  5. 点击 Connect,在弹出的 Connect to your project 页面中选择 App Frameworks,即可获取 Supabase URL 和 Supabase Key。

3. 准备数据表

create table todos ( id bigint generated by default as identity primary key, task text not null, is_complete boolean default false, inserted_at timestamp with time zone default timezone('utc'::text, now()) not null );

2. 安装 SDK

pip install supabase

3. 代码实现

import os from supabase import create_client, Client from dotenv import load_dotenv load_dotenv() url: str = os.environ.get("SUPABASE_URL") key: str = os.environ.get("SUPABASE_KEY") supabase: Client = create_client(url, key) def run_demo(): # --- 插入数据 --- print("正在插入数据...") data, count = supabase.table("todos").insert({"task": "学习 Supabase Python SDK"}).execute() # --- 查询数据 --- print("查询所有任务:") response = supabase.table("todos").select("*").execute() for row in response.data: print(f"ID: {row['id']} | 任务: {row['task']} | 完成状态: {row['is_complete']}") # --- 更新数据 --- if response.data: todo_id = response.data[0]['id'] supabase.table("todos").update({"is_complete": True}).eq("id", todo_id).execute() print(f"\n已将 ID {todo_id} 标记为完成。") # --- 再次查询数据 --- print("查询所有任务:") response = supabase.table("todos").select("*").execute() for row in response.data: print(f"ID: {row['id']} | 任务: {row['task']} | 完成状态: {row['is_complete']}") if __name__ == "__main__": run_demo()

更多内容,请参见 Supabase 官方文档