DEV Community

Matthew LaFalce
Matthew LaFalce

Posted on • Edited on

2

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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay