Duplicating Table Structure and Data with PostgreSQL

Matthew LaFalce - Aug 26 '22 - - Dev Community

A handy little feature I recently learned about is super simple to work with. Use this to copy the structure of a table:

CREATE TABLE table2 ( LIKE table1 INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES );
Enter fullscreen mode Exit fullscreen mode

Then you can copy data into your new table at your liking with:

INSERT INTO table2 SELECT * FROM table LIMIT 100;
Enter fullscreen mode Exit fullscreen mode

Happy programming!

. . . . . . . . .
Terabox Video Player