PostgreSQL supports CREATE TABLE AS
and SELECT INTO
when do I use both?
CREATE TABLE AS
-- define a new table from the results of a query
CREATE TABLE AS
creates a table and fills it with data computed by aSELECT
command. The table columns have the names and data types associated with the output columns of theSELECT
(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 definingSELECT
statement whenever it is queried.
And, then.
SELECT INTO
-- define a new table from the results of a query
SELECT INTO
creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normalSELECT
. The new table's columns have the names and data types associated with the output columns of theSELECT
.