Create schemas and tables
Create a schema or a table in a PostgreSQL cluster from the portal's Data Explorer — names, columns, types, keys, the dry run — who owns what you create, and how to drop it.
Requires: postgrescluster/dataRead, postgrescluster/schemaRead, postgrescluster/schemaWrite
Schemas and tables are created on the cluster's Data Explorer tab, which needs the Database Explorer role — see Who can use it. The cluster must be running.
Every change here goes the same way: the form writes the SQL and shows it under SQL preview, a dry run tries it in a transaction that is rolled back, and only then can you confirm it. Editing any field after the dry run means a new one.
What you create belongs to the database's owner — the user in the cluster's connection string — so your applications can use it at once, without grants.
Create a schema
- In the tree on the Data Explorer tab, choose the Create schema button at the top.
- Fill in Create schema:
- Name — lowercase letters, digits and underscores, starting with a letter or an underscore, at most 63 characters;
- Comment (optional) — up to 1024 characters;
- IF NOT EXISTS — skip silently if a schema with that name exists.
- Choose Dry run. When it passes, the sheet says Dry run passed — ready to commit.
- Choose Confirm & create. The schema appears in the tree.
With SQL, on Query or with the CLI:
stsh pg query orders-db "create schema analytics"Create a table
In the tree, choose Create table on the schema to create it in. The sheet's title, Create a new table under schema, lets you pick another schema.
Enter the table's Name, with the same rules as a schema name.
Define the Columns. The sheet starts with two:
id, abigintprimary key that numbers itself, andcreated_at, atimestamptzthat defaults tonow(). For each column:- Name — the same rules as above, unique in the table;
- Type — pick one of the common types, such as
text,varchar(255),integer,bigint,numeric(10,2),boolean,uuid,timestamptzorjsonb, or choose Custom… and write another type, optionally with a size such as(64)or(10,2); - Default — an SQL expression, such as
now()or'pending'; the field suggests one for the type; - Primary — part of the primary key. Mark several columns for a key made of all of them;
- Null — whether the column may be empty. A primary key column may not;
- Auto — the column numbers itself. Only for
smallint,integerandbigint, and not together with a Default.
Add column adds one; drag a row to reorder.
Optionally turn on IF NOT EXISTS and add a Comment (optional).
Choose Review & save for the dry run, then Confirm & create.
Foreign keys, indexes and unique constraints are not in the form; add them with SQL on Query once the table exists. A table whose primary key is a single column can be edited on Browse; see Browse and edit rows.
Drop a schema or a table
Dropping needs postgrescluster/schemaDrop, which the Database Explorer role does not include —
see Who can use it.
- In the tree, choose the drop button on the schema or the table.
- Turn on CASCADE to drop what depends on it as well — foreign keys, views and so on. Without it, the drop is refused if anything depends on it.
- Choose Dry run. The result lists the objects the drop would take with it.
- Type the object's name to confirm and choose the Drop button. This cannot be undone, other than by restoring a snapshot.
If it is refused
- Denied — missing permissions. — the result names the permissions you lack, and which of them just-in-time access could grant.
- Refused — structurally not allowed. — the statement is one the Data Explorer never runs; see Refused statements.
- An error with an SQL state, such as
[42P07], comes from PostgreSQL itself — for example, an object with that name already exists.