1SELECT INTO(7) PostgreSQL 11.3 Documentation SELECT INTO(7)
2
3
4
6 SELECT_INTO - define a new table from the results of a query
7
9 [ WITH [ RECURSIVE ] with_query [, ...] ]
10 SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
11 * | expression [ [ AS ] output_name ] [, ...]
12 INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table
13 [ FROM from_item [, ...] ]
14 [ WHERE condition ]
15 [ GROUP BY expression [, ...] ]
16 [ HAVING condition [, ...] ]
17 [ WINDOW window_name AS ( window_definition ) [, ...] ]
18 [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
19 [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
20 [ LIMIT { count | ALL } ]
21 [ OFFSET start [ ROW | ROWS ] ]
22 [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
23 [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
24
26 SELECT INTO creates a new table and fills it with data computed by a
27 query. The data is not returned to the client, as it is with a normal
28 SELECT. The new table's columns have the names and data types
29 associated with the output columns of the SELECT.
30
32 TEMPORARY or TEMP
33 If specified, the table is created as a temporary table. Refer to
34 CREATE TABLE (CREATE_TABLE(7)) for details.
35
36 UNLOGGED
37 If specified, the table is created as an unlogged table. Refer to
38 CREATE TABLE (CREATE_TABLE(7)) for details.
39
40 new_table
41 The name (optionally schema-qualified) of the table to be created.
42
43 All other parameters are described in detail under SELECT(7).
44
46 CREATE TABLE AS (CREATE_TABLE_AS(7)) is functionally similar to SELECT
47 INTO. CREATE TABLE AS is the recommended syntax, since this form of
48 SELECT INTO is not available in ECPG or PL/pgSQL, because they
49 interpret the INTO clause differently. Furthermore, CREATE TABLE AS
50 offers a superset of the functionality provided by SELECT INTO.
51
52 To add OIDs to the table created by SELECT INTO, enable the
53 default_with_oids configuration variable. Alternatively, CREATE TABLE
54 AS can be used with the WITH OIDS clause.
55
57 Create a new table films_recent consisting of only recent entries from
58 the table films:
59
60 SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
61
63 The SQL standard uses SELECT INTO to represent selecting values into
64 scalar variables of a host program, rather than creating a new table.
65 This indeed is the usage found in ECPG (see Chapter 36) and PL/pgSQL
66 (see Chapter 43). The PostgreSQL usage of SELECT INTO to represent
67 table creation is historical. It is best to use CREATE TABLE AS for
68 this purpose in new code.
69
71 CREATE TABLE AS (CREATE_TABLE_AS(7))
72
73
74
75PostgreSQL 11.3 2019 SELECT INTO(7)