Creating Tables
You can create tables through the Dashboard, SQL Editor, or migrations.Using the Dashboard
- Navigate to the Table Editor in your Supabase Dashboard
- Click New Table
- Enter the table name and add columns
- Configure primary keys and constraints
- Click Save
Using SQL
Using Migrations
For production workflows, use migrations to track schema changes:supabase/migrations/20241205075911_create_movies_table.sql
Data Types
PostgreSQL supports a rich set of data types:Common Data Types
| Type | Description | Example |
|---|---|---|
text | Variable-length text | 'Hello World' |
varchar(n) | Variable-length text with limit | 'Limited' |
integer / int | 4-byte integer | 42 |
bigint | 8-byte integer | 9223372036854775807 |
numeric(p,s) | Exact decimal | 99.99 |
boolean | True or false | true |
timestamptz | Timestamp with timezone | 2024-03-04 10:30:00+00 |
uuid | Universally unique identifier | a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11 |
jsonb | Binary JSON data | {"key": "value"} |
Advanced Data Types
Primary Keys
Every table should have a primary key to uniquely identify each row.Auto-incrementing Identity
UUID Primary Keys
Composite Primary Keys
Constraints
Constraints enforce data integrity rules.NOT NULL Constraint
UNIQUE Constraint
CHECK Constraint
DEFAULT Values
Foreign Keys & Relationships
Foreign keys create relationships between tables.One-to-Many Relationship
Many-to-Many Relationship
Foreign Key Actions
Inserting Data
Single Row Insert
Multiple Row Insert
Insert with Returning
Using JavaScript Client
Updating Data
Deleting Data
Views
Views provide reusable queries and can simplify complex data access.Creating Views
Materialized Views
Materialized views cache query results for better performance:Table Modifications
Add Columns
Modify Columns
Drop Columns
Rename Tables and Columns
Real Example from Supabase Source
Here’s a real table from the Supabase source code:Best Practices
Use meaningful table names
Use meaningful table names
- Use lowercase with underscores:
user_profiles, notUserProfiles - Use plural nouns for tables:
movies,users,orders - Avoid generic names:
data,info,temp
Always define primary keys
Always define primary keys
Every table should have a primary key for uniqueness and indexing.
Use appropriate data types
Use appropriate data types
- Use
timestamptzfor timestamps (includes timezone) - Use
numericfor money (exact precision) - Use
textfor variable-length strings - Use
jsonbfor flexible structured data
Add timestamps
Add timestamps
Include
created_at and updated_at columns for audit trails.Enable Row Level Security (RLS)
Enable Row Level Security (RLS)
Protect your data with row-level security policies.
Next Steps
Functions
Create database functions for complex logic
Triggers
Automate actions with database triggers
Migrations
Manage schema changes with migrations
Indexes
Optimize query performance with indexes
