info > CREATE_TABLE_AS(7)

๐Ÿ“› NAME

CREATE_TABLE_AS โ€” define a new table from the results of a query

๐Ÿš€ Quick Reference

Use CaseCommandDescription
๐Ÿ“ฅ Create table from query resultCREATE 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 copyCREATE TABLE films2 AS TABLE films;Copies all rows from source table
๐Ÿงช Temporary table with auto-dropCREATE 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 writesCREATE UNLOGGED TABLE log_fast AS SELECT * FROM logs;Creates an unlogged table (no WAL, not crash-safe)

๐Ÿ“‹ SYNOPSIS

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 ]

๐Ÿ“ 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 (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.

โš™๏ธ PARAMETERS

๐ŸŒ GLOBAL or LOCAL

Ignored for compatibility. Use of these keywords is deprecated; refer to CREATE TABLE (CREATE_TABLE(7)) for details.

๐Ÿ” TEMPORARY or TEMP

If specified, the table is created as a temporary table. Refer to CREATE TABLE (CREATE_TABLE(7)) for details.

๐Ÿšซ UNLOGGED

If specified, the table is created as an unlogged table. Refer to CREATE TABLE (CREATE_TABLE(7)) for details.

โš ๏ธ IF NOT EXISTS

Do not throw an error if a relation with the same name already exists; simply issue a notice and leave the table unmodified.

๐Ÿท๏ธ table_name

The name (optionally schema-qualified) of the 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.

๐Ÿ’พ USING method

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.

๐Ÿ”ง WITH ( storage_parameter [= value] [, ... ] )

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.

๐Ÿšซ WITHOUT OIDS

This is backward-compatible syntax for declaring a table WITHOUT OIDS, creating a table WITH OIDS is not supported anymore.

๐Ÿ”„ ON COMMIT

The behavior of temporary tables at the end of a transaction block can be controlled using ON COMMIT. The three options are:

๐Ÿ’ฝ TABLESPACE tablespace_name

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.

๐Ÿ“Š query

A SELECT, TABLE, or VALUES command, or an EXECUTE command that runs a prepared SELECT, TABLE, or VALUES query.

๐Ÿ“ฆ WITH [ NO ] DATA

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.

๐Ÿ“Œ NOTES

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.

๐Ÿ’ก 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 >= '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');

๐Ÿ”— COMPATIBILITY

CREATE TABLE AS conforms to the SQL standard. The following are nonstandard extensions:

๐Ÿ‘€ SEE ALSO

CREATE MATERIALIZED VIEW (CREATE_MATERIALIZED_VIEW(7)), CREATE TABLE (CREATE_TABLE(7)), EXECUTE(7), SELECT(7), SELECT INTO (SELECT_INTO(7)), VALUES(7)

CREATE_TABLE_AS(7)
๐Ÿ“› NAME ๐Ÿš€ Quick Reference ๐Ÿ“‹ SYNOPSIS ๐Ÿ“ DESCRIPTION โš™๏ธ PARAMETERS
๐ŸŒ GLOBAL or LOCAL ๐Ÿ” TEMPORARY or TEMP ๐Ÿšซ UNLOGGED โš ๏ธ IF NOT EXISTS ๐Ÿท๏ธ table_name ๐Ÿ—‚๏ธ column_name ๐Ÿ’พ USING method ๐Ÿ”ง WITH ( storage_parameter [= value] [, ... ] ) ๐Ÿšซ WITHOUT OIDS ๐Ÿ”„ ON COMMIT ๐Ÿ’ฝ TABLESPACE tablespace_name ๐Ÿ“Š query ๐Ÿ“ฆ WITH [ NO ] DATA
๐Ÿ“Œ NOTES ๐Ÿ’ก EXAMPLES ๐Ÿ”— COMPATIBILITY ๐Ÿ‘€ SEE ALSO

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)
Valid XHTML 1.0 Transitional!Valid CSS!