PostgreSQL – Create Table AS using a query

CREATE TABLE AS can be used to create a new table from the results of a query.

Syntax:

CREATE TABLE tablename AS
SELECT * FROM table1;

New table will be created with the same column names and data types of the columns used in the select query.

By default, the new table is created with data; Data being the result set of the select query. You can create the table without data using the WITH NO DATA clause as shown below. In this case, an empty table will be created without any data.

CREATE TABLE tablename AS
SELECT * FROM table1
WITH NO DATA;
See also  PostgreSQL - List installed extensions