This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a demonstration on how to copy data from one table to another table using INSERT INTO SELECT in PostgreSQL.
This article may help the beginner of PostgreSQL, because moving or copying data within the database which is the ubiquitous task.
Use INSERT INTO SELECT statement, for this exercise:
Create two sample tables:
1 2 3 4 5 |
CREATE TABLE tbl_A (ID INT); CREATE TABLE tbl_B (ID INT); |
Insert data into tbl_A:
1 2 3 |
INSERT INTO tbl_A VALUES (1),(2),(3); |
Copy data into tbl_B:
1 2 |
INSERT INTO tbl_B SELECT *FROM tbl_A; |
Check the result:
1 2 3 4 5 6 7 |
SELECT *FROM tbl_B; id -------- 1 2 3 |
Don’t try a below option:
1 2 3 4 5 |
SELECT * INTO tbl_B FROM tbl_A; ********** Error ********** ERROR: relation "tbl_b" already exists SQL state: 42P07 |