Create the copy of table in Postgresql (with and without data)

In this Tutorial we will see how to create the copy of table in postgresql with example. Let’s see

  • How to create the copy of table in postgresql with data.
  • How to create the copy of table in postgresql without data.
  • How to create the copy of table in postgresql with partial data (with constraints)

With an example for each

Syntax:

CREATE TABLE table_copy AS  TABLE existing_table;

The table that we use is

odetails:

Create the copy of table in Postgresql 1

 

Create the copy of table in postgresql with data.

CREATE TABLE odetails_copyfull AS TABLE odetails;

The complete copy of the table is created and named as  “odetails_copyfull

So the resultant table is

odetails_copyfull:

Create the copy of table in Postgresql 1

 

Create the copy of table in postgresql without data.

CREATE TABLE odetails_copy_nodata AS 
TABLE odetails with NO DATA;

The copy of the table is created without data and named asodetails_copy_nodata

So the resultant table is

odetails_copy_nodata:

Create the copy of table in Postgresql 2

 

Create the copy of table in postgresql with partial data


CREATE TABLE odetails_copy_partial AS 
SELECT * FROM Odetails 
WHERE quantity=1;

The partial copy of the table with condition quantity =1 is created and named asodetails_copy_partial

So the resultant table is

odetails_copy_partial

Create the copy of table in Postgresql 3