Loading...

Section

Creating Your First Database Table

Part of The Prince Academy's AI & DX engineering stack.

Follow The Prince Academy Inc.

Now that your Supabase project is up and running, it's time to build the foundation of your application: your database tables. Think of tables as organized collections of data, much like spreadsheets. Each row in a table represents a single record, and each column represents a specific piece of information about that record.

We'll start by creating a simple 'todos' table to store our to-do items. This table will have a few columns to keep track of each to-do.

Navigate to the 'Table editor' section in your Supabase project dashboard. You'll see a '+' button to create a new table. Click it!

You'll be presented with an interface to define your table schema. Let's add the following columns:

  1. id: This will be our primary key, a unique identifier for each to-do. We'll set it to be a UUID (Universally Unique Identifier) and auto-generate it. This ensures every to-do has a unique ID. For 'name', enter 'id'. For 'type', select 'UUID'. Under 'Default value', choose 'gen_random_uuid()'. Under 'Primary Key', check the box.
  1. task: This column will store the actual text of the to-do item. For 'name', enter 'task'. For 'type', select 'TEXT'. Check the 'Not Null' box, as every to-do should have a task description.
  1. is_complete: This will be a boolean (true/false) value indicating whether the to-do item has been completed. For 'name', enter 'is_complete'. For 'type', select 'BOOLEAN'. Set the 'Default value' to 'false', as new to-dos are not complete by default.
  1. created_at: This timestamp will record when the to-do item was created. For 'name', enter 'created_at'. For 'type', select 'TIMESTAMPTZ' (Timestamp with Time Zone). Under 'Default value', choose 'now()'. This will automatically populate the creation time.

Once you've defined these columns, give your table a name. Let's call it 'todos'. Click the 'Save' button.

Congratulations! You've just created your first database table in Supabase. You can now see your 'todos' table listed in the 'Table editor', ready for you to add data.

graph TD; A(Dashboard) --> B(Table editor); B --> C{Create New Table}; C --> D(Define Schema); D --> E(Add Columns: id, task, is_complete, created_at); E --> F(Name Table: todos); F --> G(Save Table); G --> H(Table Editor - todos listed);