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

前端 未结 3 684
情书的邮戳
情书的邮戳 2021-01-30 10:11

We use copy command to copy data of one table to a file outside database.

Is it possible to copy data of one table to another table using command.

If yes can anyon

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 10:50

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

    CREATE TABLE mycopy AS
    SELECT * FROM mytable;
    

    or

    CREATE TABLE mycopy (LIKE mytable INCLUDING ALL);
    
    INSERT INTO mycopy
    SELECT * 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.

提交回复
热议问题