How do I copy data from one table to another in postgres using copy command How do I copy data from one table to another in postgres using copy command sql sql

How do I copy data from one table to another in postgres using copy command


You cannot easily do that, but there's also no need to do so.

CREATE TABLE mycopy ASSELECT * FROM mytable;

or

CREATE TABLE mycopy (LIKE mytable INCLUDING ALL);INSERT INTO mycopySELECT * FROM mytable;

If you need to select only some columns or reorder them, you can do this:

INSERT INTO mycopy(colA, colB)SELECT col1, col2 FROM mytable;

You can also do a selective pg_dump and restore of just the target table.


If the columns are the same (names and datatypes) in both tables then you can use the following

INSERT INTO receivingtable (SELECT * FROM sourcetable WHERE column1='parameter' AND column2='anotherparameter');


Suppose there is already a table and you want to copy all records from this table to another table which is not currently present in the database then following query will do this task for you:

SELECT * into public."NewTable" FROM public."ExistingTable";