PostgreSQL Create table from existing table

In this article, we are going to see how to Create PostgreSQL table structure from existing table with examples.

PostgreSQL query to copy the structure of an existing table to create another table.

create table table_name as select * from exsting_table_name where 1=2;

PostgreSQL Create table from existing table example:

CREATE TABLE oil as SELECT * FROM t_oil where 1=2;

PostgreSQL Create table from existing table

Lets verify the structure of both tables.

\d oil Table "public.oil" Column | Type | Collation | Nullable | Default -------------+---------+-----------+----------+--------- region | text | | | country | text | | | year | integer | | | production | integer | | | consumption | integer | | | \d t_oil Table "public.t_oil" Column | Type | Collation | Nullable | Default -------------+---------+-----------+----------+--------- region | text | | | country | text | | | year | integer | | | production | integer | | | consumption | integer | | |

Both tables structure is same.