Duplicate PostgreSQL table


Use CREATE TABLE to duplicate a PostgreSQL table with data:

CREATE TABLE duplicate_tablename
AS TABLE original_tablename;

However, this doesn’t copy the table structure.

To copy a table with structure:

CREATE TABLE duplicate_tablename (
  LIKE original_tablename
  INCLUDING DEFAULTS
  INCLUDING CONSTRAINTS
  INCLUDING INDEXES
);

Then insert the data:

INSERT INTO duplicate_tablename
SELECT *
FROM original_tablename;


Please support this site and join our Discord!