PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL Copy Table: A Step-by-Step Guide with Practical Examples

PostgreSQL Copy Table: A Step-by-Step Guide with Practical Examples

PostgreSQL Copy TableSummary: in this tutorial, we will show you step by step how to copy an existing table including table structure and data by using the various forms of PostgreSQL copy table statement.

Introduction to PostgreSQL copy table statement

To copy a table completely, including both table structure and data, you use the following statement:

1
2
CREATE TABLE new_table AS
TABLE existing_table;

To copy a table structure without data, you add the WITH NO DATA clause to the CREATE TABLE statement as follows:

1
2
3
CREATE TABLE new_table AS
TABLE existing_table
WITH NO DATA;

To copy a table with partial data from an existing table, you use the following statement:

1
2
3
4
5
6
7
CREATE TABLE new_table AS
SELECT
*
FROM
    existing_table
WHERE
    condition;

The condition in the WHERE clause of the query defines which rows of the existing table will be copied to the new table.

Note that all the statement above copy table structure and data but do not copy indexes and constraints of the existing table.

PostgreSQL copy table example

The following statement creates a new table named contacts for the demonstration:

1
2
3
4
5
6
CREATE TABLE contacts(
    id SERIAL PRIMARY KEY,
    first_name VARCHAR NOT NULL,
    last_name VARCHAR NOT NULL,
    email VARCHAR NOT NULL UNIQUE
);

In this table, we have two indexes: one index for the primary key and another for the UNIQUE constraint.

Let’s insert some rows into the contacts table:

1
2
3
INSERT INTO contacts(first_name, last_name, email)
VALUES('John','Doe','[email protected]'),
      ('David','William','[email protected]');

To copy the contacts to a new table, for example, contacts_backup table, you use the following statement:

1
2
CREATE TABLE contact_backup
AS TABLE contacts;

This statement creates a new table named contact_backup whose structure is the same as the contacts table. In addition, it copies data from the contacts table to the contact_backup table.

Let’s check the data of the contact_backup table by using the following SELECT statement:

1
2
3
4
5
6
7
SELECT * FROM contact_backup;
 
id | first_name | last_name |                email
----+------------+-----------+--------------------------------------
  1 | John       | Doe       | john.doe@postgresqltutorial.com
  2 | David      | William   | david.william@postgresqltutorial.com
(2 rows)

It returns two rows as expected.

To examine the structure of the contact_backup table:

1
2
3
4
5
6
7
8
test=# \d contact_backup;
       Table "public.contact_backup"
   Column   |       Type        | Modifiers
------------+-------------------+-----------
id         | integer           |
first_name | character varying |
last_name  | character varying |
email      | character varying |

As you can see in the output, the structure of the contact_backup table is the same as the contacts table except for the indexes.

To add the primary key and UNIQUE constraints to the contact_backup table, you use the following ALTER TABLE statements:

1
2
ALTER TABLE contact_backup ADD PRIMARY KEY(id);
ALTER TABLE contact_backup ADD UNIQUE(email);

To view structure of the contact_backup table again, you use \d command:

1
2
3
4
5
6
7
8
9
10
11
test=# \d contact_backup;
       Table "public.contact_backup"
   Column   |       Type        | Modifiers
------------+-------------------+-----------
id         | integer           | not null
first_name | character varying |
last_name  | character varying |
email      | character varying |
Indexes:
    "contact_backup_pkey" PRIMARY KEY, btree (id)
    "contact_backup_email_key" UNIQUE CONSTRAINT, btree (email)

In this tutorial, you have learned how to copy an existing table to a new one using the various forms PostgreSQL copy table statement.

Previous Tutorial: PostgreSQL TRUNCATE TABLE
Next Tutorial: PostgreSQL Primary Key

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.