Completely copying a postgres table with SQL

前端 未结 7 1254
春和景丽
春和景丽 2021-01-30 06:13

DISCLAIMER: This question is similar to the stack overflow question here, but none of those answers work for my problem, as I will explain later.

I\'m t

7条回答
  •  时光取名叫无心
    2021-01-30 07:04

    The create table as feature in PostgreSQL may now be the answer the OP was looking for.

    https://www.postgresql.org/docs/9.5/static/sql-createtableas.html

    create table my_table_copy as
      select * from my_table
    

    This will create an identical table with the data.

    Adding with no data will copy the schema without the data.

    create table my_table_copy as
      select * from my_table
    with no data
    

    This will create the table with all the data, but without indexes and triggers etc.


    create table my_table_copy (like my_table including all)

    The create table like syntax will include all triggers, indexes, constraints, etc. But not include data.

提交回复
热议问题