diff --git a/apps/docs/content/guides/database/tables.mdx b/apps/docs/content/guides/database/tables.mdx index 5132939f310..cbfa85a0d2f 100644 --- a/apps/docs/content/guides/database/tables.mdx +++ b/apps/docs/content/guides/database/tables.mdx @@ -76,9 +76,9 @@ and run the SQL queries yourself. 1. Go to the [Table Editor](/dashboard/project/_/editor) page in the Dashboard. -2. Click **New Table** and create a table with the name `todos`. -3. Click **Save**. -4. Click **New Column** and create a column with the name `task` and type `text`. +2. Click **New table**. +3. Enter `movies` in the **Name** field. +4. Under **Columns**, click **Add column** and enter `name` with type `text`, then add `description` with type `text`. Leave the `id` and `created_at` columns as the editor created them. 5. Click **Save**. @@ -88,7 +88,8 @@ and run the SQL queries yourself. create table movies ( id bigint generated by default as identity primary key, name text, - description text + description text, + created_at timestamptz default now() ); ``` @@ -276,27 +277,27 @@ await supabase.From().Insert(movies); #### Bulk data loading When inserting large data sets, use Postgres's [COPY](https://www.postgresql.org/docs/current/sql-copy.html) command. -This loads data directly from a file into a table. Several file formats are available for copying data, including text, CSV, binary, and JSON. +This loads data directly from a file into a table. `COPY` accepts text, CSV, and binary input. For example, to load a CSV file into your `movies` table: ```text ./movies.csv -"The Empire Strikes Back", "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda." -"Return of the Jedi", "After a daring mission to rescue Han Solo from Jabba the Hutt, the Rebels dispatch to Endor to destroy the second Death Star." +"The Empire Strikes Back","After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda." +"Return of the Jedi","After a daring mission to rescue Han Solo from Jabba the Hutt, the Rebels dispatch to Endor to destroy the second Death Star." ``` -[Connect](/docs/guides/database/connecting-to-postgres#direct-connection) to your database directly and load the file with the `COPY` command: +Set `DATABASE_URL` to your [direct connection string](/docs/guides/database/connecting-to-postgres#direct-connection), then load the file with the `COPY` command. Name the columns the file contains, so Postgres doesn't expect a value for `id`: ```bash -psql -h DATABASE_URL -p 5432 -d postgres -U postgres \ - -c "\COPY movies FROM './movies.csv';" +psql "$DATABASE_URL" \ + -c "\COPY movies (name, description) FROM './movies.csv' WITH (FORMAT csv);" ``` -You can also use the `DELIMITER`, `HEADER`, and `FORMAT` options as defined in the Postgres [COPY](https://www.postgresql.org/docs/current/sql-copy.html) docs. +You can also pass options such as `DELIMITER` and `HEADER`, as defined in the Postgres [COPY](https://www.postgresql.org/docs/current/sql-copy.html) docs. `HEADER` skips the first line of the file, so use it only when that line names the columns: ```bash -psql -h DATABASE_URL -p 5432 -d postgres -U postgres \ - -c "\COPY movies FROM './movies.csv' WITH DELIMITER ',' CSV HEADER" +psql "$DATABASE_URL" \ + -c "\COPY movies (name, description) FROM './movies-with-header.csv' WITH (FORMAT csv, HEADER, DELIMITER ';');" ``` If you receive an error `FATAL: password authentication failed for user "postgres"`, reset your database password in **Database Settings** and try again. @@ -326,12 +327,6 @@ For example, consider this situation: - An `actor` can perform in several movies. ```sql -create table movies ( - id bigint generated by default as identity primary key, - name text, - description text -); - create table actors ( id bigint generated by default as identity primary key, name text @@ -411,7 +406,7 @@ width={1600} height={1145} /> -If you don't explicitly pass a schema when creating a table, Postgres creates the table in the `public` schema. +If you don't explicitly pass a schema when creating a table, Postgres creates the table in the first schema in the current [`search_path`](https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH). The default path is `"$user", public`, so on a new project that's the `public` schema. You can create schemas to organize tables. For example, you might want a private schema that's hidden from your API: