mirror of
https://github.com/supabase/supabase.git
synced 2026-07-05 00:14:35 +08:00
136 lines
9.0 KiB
Plaintext
136 lines
9.0 KiB
Plaintext
---
|
|
id: tables
|
|
title: Tables
|
|
description: Creating and using Postgres tables.
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs';
|
|
import TabsPanel from '@theme/TabsPanel';
|
|
|
|
|
|
In Postgres, data is stored in "tables".
|
|
Tables are similar to excel spreadsheets. They contain columns & rows of data. There are a few key differences from a spreadsheet however:
|
|
|
|
- Every column is a strict type of data. When you set up a column, you must define the "data type".
|
|
- Tables can be "joined" together. For example you can have a `users` table, which is joined to a `teams` table (because users belong to teams).
|
|
|
|
For example, this table has 3 "columns" (`id`, `name`, `description`) and 4 "rows" of data:
|
|
|
|
| `id` | `name` | `description` |
|
|
| ---- | ---- | ----------- |
|
|
| 1 | Toy Story | When a new toy called "Forky" joins Woody and the gang, a road trip alongside old and new friends reveals how big the world can be for a toy. |
|
|
| 2 | Monsters, Inc. | In order to power the city, monsters have to scare children so that they scream. However, the children are toxic to monsters. |
|
|
| 3 | Finding Nemo | After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home. |
|
|
| 4 | WALL-E | In the distant future, a small waste-collecting robot inadvertently embarks on a space journey that will ultimately decide the fate of mankind. |
|
|
|
|
|
|
## Creating Tables
|
|
|
|
Supabase provides several options for creating tables. You can use the Table Editor or create them directly using SQL.
|
|
We provide a SQL editor within the Dashboard, or you can [connect](/docs/guides/database/connecting/connecting-to-postgres) to your database
|
|
and run the SQL queries yourself.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="UI"
|
|
values={[
|
|
{label: 'UI', value: 'UI'},
|
|
{label: 'SQL', value: 'SQL'},
|
|
]}>
|
|
<TabsPanel id="UI" label="UI">
|
|
|
|
```sh
|
|
1. Go to the "Table editor" section.
|
|
2. Click "New Table".
|
|
3. Enter the table name "todos".
|
|
4. Click "Save".
|
|
5. Click "New Column".
|
|
6. Enter the column name "task" and make the type "text".
|
|
7. Click "Save".
|
|
```
|
|
|
|
<video width="99%" muted playsInline controls="true">
|
|
<source src="/docs/videos/api/api-create-table-sm.mp4" type="video/mp4" muted playsInline />
|
|
</video>
|
|
|
|
</TabsPanel>
|
|
<TabsPanel id="SQL" label="SQL">
|
|
|
|
```sql
|
|
-- Create a table called "todos" with a column to store tasks.
|
|
|
|
create table todos (
|
|
id bigint generated by default as identity primary key,
|
|
task text check (char_length(task) > 3)
|
|
);
|
|
```
|
|
|
|
</TabsPanel>
|
|
</Tabs>
|
|
|
|
|
|
### Tips
|
|
|
|
- It is best practice to use lowercase and underscores when naming tables. For example: `table_name`, not `Table Name`.
|
|
- Tables belong to `schemas`. If you don't explicitly pass the schema, Postgres will assume that you want to create the table in the `public` schema.
|
|
|
|
|
|
### Data types
|
|
|
|
Every column is a predefined type. PostgreSQL provides many [default types](https://www.postgresql.org/docs/current/datatype.html), and you can even design your own (or use extensions)
|
|
if the default types don't fit your needs.
|
|
|
|
<details>
|
|
<summary>Show/Hide default data types</summary>
|
|
|
|
|
|
| `Name` | `Aliases` | `Description` |
|
|
| ------------------------------------------- | -------------------- | ----------------------------------------------------------------- |
|
|
| bigint | int8 | signed eight-byte integer |
|
|
| bigserial | serial8 | autoincrementing eight-byte integer |
|
|
| bit | | fixed-length bit string |
|
|
| bit varying | varbit | variable-length bit string |
|
|
| boolean | bool | logical Boolean (true/false) |
|
|
| box | | rectangular box on a plane |
|
|
| bytea | | binary data (“byte array”) |
|
|
| character | char | fixed-length character string |
|
|
| character varying | varchar | variable-length character string |
|
|
| cidr | | IPv4 or IPv6 network address |
|
|
| circle | | circle on a plane |
|
|
| date | | calendar date (year, month, day) |
|
|
| double precision | float8 | double precision floating-point number (8 bytes) |
|
|
| inet | | IPv4 or IPv6 host address |
|
|
| integer | int, int4 | signed four-byte integer |
|
|
| interval \[ fields \] | | time span |
|
|
| json | | textual JSON data |
|
|
| jsonb | | binary JSON data, decomposed |
|
|
| line | | infinite line on a plane |
|
|
| lseg | | line segment on a plane |
|
|
| macaddr | | MAC (Media Access Control) address |
|
|
| macaddr8 | | MAC (Media Access Control) address (EUI-64 format) |
|
|
| money | | currency amount |
|
|
| numeric | decimal | exact numeric of selectable precision |
|
|
| path | | geometric path on a plane |
|
|
| pg\_lsn | | PostgreSQL Log Sequence Number |
|
|
| pg\_snapshot | | user-level transaction ID snapshot |
|
|
| point | | geometric point on a plane |
|
|
| polygon | | closed geometric path on a plane |
|
|
| real | float4 | single precision floating-point number (4 bytes) |
|
|
| smallint | int2 | signed two-byte integer |
|
|
| smallserial | serial2 | autoincrementing two-byte integer |
|
|
| serial | serial4 | autoincrementing four-byte integer |
|
|
| text | | variable-length character string |
|
|
| time \[ without time zone \] | | time of day (no time zone) |
|
|
| time with time zone | timetz | time of day, including time zone |
|
|
| timestamp \[ without time zone \] | | date and time (no time zone) |
|
|
| timestamp with time zone | timestamptz | date and time, including time zone |
|
|
| tsquery | | text search query |
|
|
| tsvector | | text search document |
|
|
| txid\_snapshot | | user-level transaction ID snapshot (deprecated; see pg\_snapshot) |
|
|
| uuid | | universally unique identifier |
|
|
| xml | | XML data |
|
|
|
|
|
|
</details>
|
|
|