2 .\" Title: CREATE TABLE AS
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE TABLE AS" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_TABLE_AS \- define a new table from the results of a query
35 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \fItable_name\fR
36 [ (\fIcolumn_name\fR [, \&.\&.\&.] ) ]
37 [ USING \fImethod\fR ]
38 [ WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) | WITHOUT OIDS ]
39 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
40 [ TABLESPACE \fItablespace_name\fR ]
47 creates a table and fills it with data computed by a
49 command\&. The table columns have the names and data types associated with the output columns of the
51 (except that you can override the column names by giving an explicit list of new column names)\&.
54 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
56 statement whenever it is queried\&.
61 privilege on the schema used for the table\&.
66 Ignored for compatibility\&. Use of these keywords is deprecated; refer to
67 CREATE TABLE (\fBCREATE_TABLE\fR(7))
73 If specified, the table is created as a temporary table\&. Refer to
74 CREATE TABLE (\fBCREATE_TABLE\fR(7))
80 If specified, the table is created as an unlogged table\&. Refer to
81 CREATE TABLE (\fBCREATE_TABLE\fR(7))
87 Do not throw an error if a relation with the same name already exists; simply issue a notice and leave the table unmodified\&.
92 The name (optionally schema\-qualified) of the table to be created\&.
97 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\&.
102 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
105 for more information\&. If this option is not specified, the default table access method is chosen for the new table\&. See
106 default_table_access_method
107 for more information\&.
110 WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
112 This clause specifies optional storage parameters for the new table; see
115 CREATE TABLE (\fBCREATE_TABLE\fR(7))
116 documentation for more information\&. For backward\-compatibility the
118 clause for a table can also include
120 to specify that rows of the new table should contain no OIDs (object identifiers),
122 is not supported anymore\&.
127 This is backward\-compatible syntax for declaring a table
128 WITHOUT OIDS, creating a table
130 is not supported anymore\&.
135 The behavior of temporary tables at the end of a transaction block can be controlled using
136 ON COMMIT\&. The three options are:
140 No special action is taken at the ends of transactions\&. This is the default behavior\&.
145 All rows in the temporary table will be deleted at the end of each transaction block\&. Essentially, an automatic
147 is done at each commit\&.
152 The temporary table will be dropped at the end of the current transaction block\&.
156 TABLESPACE \fItablespace_name\fR
159 \fItablespace_name\fR
160 is the name of the tablespace in which the new table is to be created\&. If not specified,
164 if the table is temporary\&.
175 command that runs a prepared
184 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\&.
188 This command is functionally similar to
189 SELECT INTO (\fBSELECT_INTO\fR(7)), but it is preferred since it is less likely to be confused with other uses of the
191 syntax\&. Furthermore,
192 \fBCREATE TABLE AS\fR
193 offers a superset of the functionality offered by
199 consisting of only recent entries from the table
206 CREATE TABLE films_recent AS
207 SELECT * FROM films WHERE date_prod >= \*(Aq2002\-01\-01\*(Aq;
213 To copy a table completely, the short form using the
215 command can also be used:
221 CREATE TABLE films2 AS
228 Create a new temporary table
229 films_recent, consisting of only recent entries from the table
230 films, using a prepared statement\&. The new table will be dropped at commit:
236 PREPARE recentfilms(date) AS
237 SELECT * FROM films WHERE date_prod > $1;
238 CREATE TEMP TABLE films_recent ON COMMIT DROP AS
239 EXECUTE recentfilms(\*(Aq2002\-01\-01\*(Aq);
246 \fBCREATE TABLE AS\fR
249 standard\&. The following are nonstandard extensions:
259 The standard requires parentheses around the subquery clause; in
260 PostgreSQL, these parentheses are optional\&.
273 clause is required; in PostgreSQL it is optional\&.
285 handles temporary tables in a way rather different from the standard; see
286 CREATE TABLE (\fBCREATE_TABLE\fR(7))
302 extension; storage parameters are not in the standard\&.
315 concept of tablespaces is not part of the standard\&. Hence, the clause
320 CREATE MATERIALIZED VIEW (\fBCREATE_MATERIALIZED_VIEW\fR(7)), CREATE TABLE (\fBCREATE_TABLE\fR(7)), \fBEXECUTE\fR(7), \fBSELECT\fR(7), SELECT INTO (\fBSELECT_INTO\fR(7)), \fBVALUES\fR(7)