Article
1 comment

PostgreSQL: Copy a table structure and data

Once again a short notice about some nifty PostgreSQL feature you might not be aware of. It’s quire simple to copy the structure of a table with:

CREATE TABLE table2 ( like table1 INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES );

Now you can copy data with or without some sort of filter:

INSERT INTO table2 SELECT * FROM table LIMIT 100;

Hope that helps.

1 Comment so far

  1. I have a aws postgres rds, under this have two database, I want to copy one table data along with constraint and indexes from one database to another database, facing error like constraint and fkey error while importing the table data from one table to another after exporting. I heard some XML data solution to resolve this issue and may be possible by exporting the XML data and importing the same into another table but not sure about this option,. Can anyone suggest if you have done something similar for aws postgres rds.

    Reply

Leave a Reply

Required fields are marked *.