DEV Community

Matthew LaFalce
Matthew LaFalce

Posted on • Updated on

Duplicating Table Structure and Data with PostgreSQL

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!

Top comments (0)