CREATE_TABLE_AS โ define a new table from the results of a query
| Use Case | Command | Description |
|---|---|---|
| ๐ฅ Create table from query result | CREATE TABLE films_recent AS SELECT * FROM films WHERE date_prod >= '2002-01-01'; | Creates a new table filled with data from a SELECT query |
| ๐ Copy table structure (no data) | CREATE TABLE films2 AS TABLE films WITH NO DATA; | Creates an empty table with same structure as source |
| ๐ Quick full copy | CREATE TABLE films2 AS TABLE films; | Copies all rows from source table |
| ๐งช Temporary table with auto-drop | CREATE TEMP TABLE temp_films ON COMMIT DROP AS EXECUTE recentfilms('2002-01-01'); | Creates a temporary table that drops at transaction end |
| โ๏ธ Unlogged table for faster writes | CREATE UNLOGGED TABLE log_fast AS SELECT * FROM logs; | Creates an unlogged table (no WAL, not crash-safe) |
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name
[ (column_name [, ...] ) ]
[ USING method ]
[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]
AS query
[ WITH [ NO ] DATA ]
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 (except that you can override the column names by giving an explicit list of new column names).
CREATE TABLE AS bears some resemblance to creating a view, but it is really quite different: it 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. In contrast, a view re-evaluates its defining SELECT statement whenever it is queried.
CREATE TABLE AS requires CREATE privilege on the schema used for the table.
Ignored for compatibility. Use of these keywords is deprecated; refer to CREATE TABLE (CREATE_TABLE(7)) for details.
If specified, the table is created as a temporary table. Refer to CREATE TABLE (CREATE_TABLE(7)) for details.
If specified, the table is created as an unlogged table. Refer to CREATE TABLE (CREATE_TABLE(7)) for details.
Do not throw an error if a relation with the same name already exists; simply issue a notice and leave the table unmodified.
The name (optionally schema-qualified) of the table to be created.
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.
This optional clause specifies the table access method to use to store the contents for the new table; the method needs be an access method of type TABLE. See Chapter 61 for more information. If this option is not specified, the default table access method is chosen for the new table. See default_table_access_method for more information.
This clause specifies optional storage parameters for the new table; see Storage Parameters in the CREATE TABLE (CREATE_TABLE(7)) documentation for more information. For backward-compatibility the WITH clause for a table can also include OIDS=FALSE to specify that rows of the new table should contain no OIDs (object identifiers), OIDS=TRUE is not supported anymore.
This is backward-compatible syntax for declaring a table WITHOUT OIDS, creating a table WITH OIDS is not supported anymore.
The behavior of temporary tables at the end of a transaction block can be controlled using ON COMMIT. The three options are:
The tablespace_name is the name of the tablespace in which the new table is to be created. If not specified, default_tablespace is consulted, or temp_tablespaces if the table is temporary.
A SELECT, TABLE, or VALUES command, or an EXECUTE command that runs a prepared SELECT, TABLE, or VALUES query.
This clause specifies whether or not the data produced by the query should be copied into the new table. If not, only the table structure is copied. The default is to copy the data.
This command is functionally similar to SELECT INTO (SELECT_INTO(7)), 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 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 >= '2002-01-01';
To copy a table completely, the short form using the TABLE command can also be used:
CREATE TABLE films2 AS
TABLE films;
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('2002-01-01');
CREATE TABLE AS conforms to the SQL standard. The following are nonstandard extensions:
CREATE MATERIALIZED VIEW (CREATE_MATERIALIZED_VIEW(7)), CREATE TABLE (CREATE_TABLE(7)), EXECUTE(7), SELECT(7), SELECT INTO (SELECT_INTO(7)), VALUES(7)
Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-24 03:43 @216.73.216.102
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)