CREATE TABLE AS
Defines a new table from the results of a query.
Synopsis
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE <table_name>
[ (<column_name> [, ...] ) ]
[ WITH ( <storage_parameter> [= <value>] [, ... ] ) | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE <tablespace_name> ]
AS <query>
[ WITH [ NO ] DATA ]
[ DISTRIBUTED BY (<column> [, ... ] ) | DISTRIBUTED RANDOMLY | DISTRIBUTED REPLICATED ]
where storage_parameter is:
appendoptimized={TRUE|FALSE}
blocksize={8192-2097152}
orientation={COLUMN|ROW}
compresstype={ZLIB|ZSTD|RLE_TYPE|NONE}
compresslevel={1-19}
fillfactor={10-100}
[oids=FALSE]
Description
CREATE TABLE AS creates a table and fills it with data computed by a SELECT command.
The table columns have the names and data types associated with the output columns of the SELECT, however you can override the column names by giving an explicit list of new column names.
CREATE TABLE AS creates a new table and evaluates the query just once to fill the new table initially.
The new table will not track subsequent changes to the source tables of the query.
Parameters
| Parameter | Description |
|---|---|
GLOBAL | LOCAL |
Ignored for compatibility. These keywords are deprecated; refer to CREATE TABLE for details |
TEMPORARY | TEMP |
If specified, the new table is created as a temporary table.
Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see |
UNLOGGED |
If specified, the table is created as an unlogged table. Data written to unlogged tables is not written to the write-ahead (WAL) log, which makes them considerably faster than ordinary tables. However, the contents of an unlogged table are not replicated to mirror segment instances. Also an unlogged table is not crash-safe. After a segment instance crash or unclean shutdown, the data for the unlogged table on that segment is truncated. Any indexes created on an unlogged table are automatically unlogged as well |
table_name |
The name (optionally schema-qualified) of the new table to be created |
column_name |
The name of a column in the new table. If column names are not provided, they are taken from the output column names of the query |
WITH ( <storage_parameter>=<value> ) |
The
|
ON COMMIT |
The behavior of temporary tables at the end of a transaction block can be controlled using
|
TABLESPACE <tablespace_name> |
The |
AS query |
A |
DISTRIBUTED BY ({<column> [<opclass>]}, [ … ] ) DISTRIBUTED RANDOMLY DISTRIBUTED REPLICATED |
Used to declare the Greengage DB distribution policy for the table.
The Greengage DB server configuration parameter
|
Notes
This command is functionally similar to SELECT INTO, but it is preferred since it is less likely to be confused with other uses of the SELECT INTO syntax.
Furthermore, CREATE TABLE AS offers a superset of the functionality offered by SELECT INTO.
CREATE TABLE AS can be used for fast data loading from external table data sources.
See CREATE EXTERNAL TABLE.
Examples
Create a new table films_recent consisting of only recent entries from the table films:
CREATE TABLE films_recent AS
SELECT *
FROM films
WHERE date_prod >= '2025-01-01';
Create a new temporary table films_recent, consisting of only recent entries from the table films, using a prepared statement.
The new table will be dropped at commit:
PREPARE recentfilms(date) AS SELECT *
FROM films
WHERE date_prod > $1;
CREATE TEMP TABLE films_recent ON COMMIT DROP AS
EXECUTE recentfilms
(
'2025-01-01'
);
Compatibility
CREATE TABLE AS conforms to the SQL standard, with the following exceptions:
-
The standard requires parentheses around the subquery clause; in Greengage DB, these parentheses are optional.
-
In the standard, the
WITH [NO] DATAclause is required; in Greengage DB, it is optional. -
Greengage DB handles temporary tables differently from the standard; see CREATE TABLE for details.
-
The
WITHclause is a Greengage DB extension; neither storage parameters norOIDsare in the standard. The syntax for creating OID system columns is deprecated. -
The Greengage DB concept of tablespaces is not part of the standard. The
TABLESPACEclause is an extension.