CREATE TABLE... AS creates a new table from a .
The CREATE TABLE AS statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .
Intended use
Tables created withCREATE TABLE... AS are intended to persist the result of a query for later reuse.
This can be more efficient than a when the following two conditions are met:
- The result of the query is used as-is multiple times.
- The copy needs not be kept up-to-date with the original table over time.
CREATE TABLE t AS... with the AS OF SYSTEM TIME clause to leverage to reduce contention and improve performance.
The default rules for apply.
Required privileges
The user must have theCREATE on the parent database.
Synopsis
- Basic
- Expanded
Parameters
| Parameter | Description |
|---|---|
IF NOT EXISTS | Create a new table only if a table of the same name does not already exist in the database; if one does exist, do not return an error. Note that IF NOT EXISTS checks the table name only; it does not check if an existing table has the same columns, indexes, constraints, etc., of the new table. |
table\_name | The name of the table to create, which must be unique within its database and follow these . When the parent database is not set as the default, the name must be formatted as database.name. The and statements use a temporary table called excluded to handle uniqueness conflicts during execution. It’s therefore not recommended to use the name excluded for any of your tables. |
column\_name | The name of the column you want to use instead of the name of the column from select\_stmt. |
create\_as\_col\_qual\_list | An optional column definition, which may include and . |
family\_def | An optional . Column family names must be unique within the table but can have the same name as columns, constraints, or indexes. |
create\_as\_constraint\_def | An optional . |
select\_stmt | A to provide the data. |
opt\_persistence\_temp\_table | Defines the table as a session-scoped temporary table. For more information, see . Note that the LOCAL, GLOBAL, and UNLOGGED options are no-ops, allowed by the parser for PostgreSQL compatibility. Support for temporary tables is . |
opt\_with\_storage\_parameter\_list | A comma-separated list of . Supported parameters include fillfactor, s2\_max\_level, s2\_level\_mod, s2\_max\_cells, geometry\_min\_x, geometry\_max\_x, geometry\_min\_y, and geometry\_max\_y. The fillfactor parameter is a no-op, allowed for PostgreSQL-compatibility. For details, see . For an example, see . |
ON COMMIT PRESERVE ROWS | This clause is a no-op, allowed by the parser for PostgreSQL compatibility. CockroachDB only supports session-scoped , and does not support the clauses ON COMMIT DELETE ROWS and ON COMMIT DROP, which are used to define transaction-scoped temporary tables in PostgreSQL. |
Known limitations
The of tables created withCREATE TABLE... AS is not automatically derived from the query results. You must specify new primary keys at table creation. For examples, see .
Examples
Setup
To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:Create a table from a SELECT query
Change column names
This statement creates a copy of an existing table but with changed column names:Create a table from a VALUES clause
Create a copy of an existing table
Specify a primary key
You can specify the of a new table created from a selection query:Define column families
You can define the of a new table created from a selection query:Populate CREATE TABLE AS with historical data using AS OF SYSTEM TIME
CockroachDB supports creating a table using historical data using the clause. You can use this to create a new table based on the state of an existing table as of a specific in the past. This is useful for:
- Generating static datasets for reporting or analytical workloads without increasing contention on production tables or otherwise impacting performance.
- Analyzing data changes over time.
- Preserving historical data for regulatory or investigative purposes.
- Undoing an accidental table deletion.
The timestamp must be within the of the source table for the data to be available.
Undo an accidental table deletion
The following steps use a table from the dataset to show how to undo an accidental table deletion usingCREATE TABLE AS... AS OF SYSTEM TIME.
-
Get a timestamp before the table is deleted in an upcoming step:
-
Wait a few seconds to simulate time passing (adjust as needed):
-
Drop the original table:
-
Restore the table using the clause and the timestamp you obtained before dropping the table:

