How to copy data from one table to another using pgsql

H

How Can We Help?

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
);  

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me