Select Page

How to copy data from one table to another using pgsql

If you need to copy data from one table (table_a) into another table (table_b) using pgsql.

There are 2 methods on how to do this.

Method 1

NOTE: If you are using this method table_b should not yet exist.

If you want an exact duplicate without indexing or relying on objects use this.

SELECT * INTO table_b FROM table_a

Method 2

NOTE: This method will work if table_b already exist.

This method uses a subselect inside the INSERT statement

INSERT INTO table_b
(
SELECT
*
FROM
table_a
WHERE condition
);  

Related Articles

Last Updated On April 07, 2018

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.