1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
'\" t
.\" Title: CREATE TABLE AS
.\" Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\" Date: 2024
.\" Manual: PostgreSQL 16.2 Documentation
.\" Source: PostgreSQL 16.2
.\" Language: English
.\"
.TH "CREATE TABLE AS" "7" "2024" "PostgreSQL 16.2" "PostgreSQL 16.2 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
CREATE_TABLE_AS \- define a new table from the results of a query
.SH "SYNOPSIS"
.sp
.nf
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \fItable_name\fR
[ (\fIcolumn_name\fR [, \&.\&.\&.] ) ]
[ USING \fImethod\fR ]
[ WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE \fItablespace_name\fR ]
AS \fIquery\fR
[ WITH [ NO ] DATA ]
.fi
.SH "DESCRIPTION"
.PP
\fBCREATE TABLE AS\fR
creates a table and fills it with data computed by a
\fBSELECT\fR
command\&. The table columns have the names and data types associated with the output columns of the
\fBSELECT\fR
(except that you can override the column names by giving an explicit list of new column names)\&.
.PP
\fBCREATE TABLE AS\fR
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
\fBSELECT\fR
statement whenever it is queried\&.
.PP
\fBCREATE TABLE AS\fR
requires
CREATE
privilege on the schema used for the table\&.
.SH "PARAMETERS"
.PP
GLOBAL or LOCAL
.RS 4
Ignored for compatibility\&. Use of these keywords is deprecated; refer to
CREATE TABLE (\fBCREATE_TABLE\fR(7))
for details\&.
.RE
.PP
TEMPORARY or TEMP
.RS 4
If specified, the table is created as a temporary table\&. Refer to
CREATE TABLE (\fBCREATE_TABLE\fR(7))
for details\&.
.RE
.PP
UNLOGGED
.RS 4
If specified, the table is created as an unlogged table\&. Refer to
CREATE TABLE (\fBCREATE_TABLE\fR(7))
for details\&.
.RE
.PP
IF NOT EXISTS
.RS 4
Do not throw an error if a relation with the same name already exists; simply issue a notice and leave the table unmodified\&.
.RE
.PP
\fItable_name\fR
.RS 4
The name (optionally schema\-qualified) of the table to be created\&.
.RE
.PP
\fIcolumn_name\fR
.RS 4
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\&.
.RE
.PP
USING \fImethod\fR
.RS 4
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\ \&63
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\&.
.RE
.PP
WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
.RS 4
This clause specifies optional storage parameters for the new table; see
Storage Parameters
in the
CREATE TABLE (\fBCREATE_TABLE\fR(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\&.
.RE
.PP
WITHOUT OIDS
.RS 4
This is backward\-compatible syntax for declaring a table
WITHOUT OIDS, creating a table
WITH OIDS
is not supported anymore\&.
.RE
.PP
ON COMMIT
.RS 4
The behavior of temporary tables at the end of a transaction block can be controlled using
ON COMMIT\&. The three options are:
.PP
PRESERVE ROWS
.RS 4
No special action is taken at the ends of transactions\&. This is the default behavior\&.
.RE
.PP
DELETE ROWS
.RS 4
All rows in the temporary table will be deleted at the end of each transaction block\&. Essentially, an automatic
\fBTRUNCATE\fR
is done at each commit\&.
.RE
.PP
DROP
.RS 4
The temporary table will be dropped at the end of the current transaction block\&.
.RE
.RE
.PP
TABLESPACE \fItablespace_name\fR
.RS 4
The
\fItablespace_name\fR
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\&.
.RE
.PP
\fIquery\fR
.RS 4
A
\fBSELECT\fR,
\fBTABLE\fR, or
\fBVALUES\fR
command, or an
\fBEXECUTE\fR
command that runs a prepared
\fBSELECT\fR,
\fBTABLE\fR, or
\fBVALUES\fR
query\&.
.RE
.PP
WITH [ NO ] DATA
.RS 4
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\&.
.RE
.SH "NOTES"
.PP
This command is functionally similar to
SELECT INTO (\fBSELECT_INTO\fR(7)), but it is preferred since it is less likely to be confused with other uses of the
\fBSELECT INTO\fR
syntax\&. Furthermore,
\fBCREATE TABLE AS\fR
offers a superset of the functionality offered by
\fBSELECT INTO\fR\&.
.SH "EXAMPLES"
.PP
Create a new table
films_recent
consisting of only recent entries from the table
films:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE TABLE films_recent AS
SELECT * FROM films WHERE date_prod >= \*(Aq2002\-01\-01\*(Aq;
.fi
.if n \{\
.RE
.\}
.PP
To copy a table completely, the short form using the
TABLE
command can also be used:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE TABLE films2 AS
TABLE films;
.fi
.if n \{\
.RE
.\}
.PP
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:
.sp
.if n \{\
.RS 4
.\}
.nf
PREPARE recentfilms(date) AS
SELECT * FROM films WHERE date_prod > $1;
CREATE TEMP TABLE films_recent ON COMMIT DROP AS
EXECUTE recentfilms(\*(Aq2002\-01\-01\*(Aq);
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
\fBCREATE TABLE AS\fR
conforms to the
SQL
standard\&. The following are nonstandard extensions:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The standard requires parentheses around the subquery clause; in
PostgreSQL, these parentheses are optional\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
In the standard, the
WITH [ NO ] DATA
clause is required; in PostgreSQL it is optional\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
PostgreSQL
handles temporary tables in a way rather different from the standard; see
CREATE TABLE (\fBCREATE_TABLE\fR(7))
for details\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The
WITH
clause is a
PostgreSQL
extension; storage parameters are not in the standard\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
The
PostgreSQL
concept of tablespaces is not part of the standard\&. Hence, the clause
TABLESPACE
is an extension\&.
.RE
.SH "SEE ALSO"
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)
|