Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy certain tables from one schema to another within same DB in Postgres keeping the original schema?

I want to copy only 4 tables from schema1 to schema2 within same DB in Postgres. And would like to keep the tables in schema1 as well. Any idea how to do that in pgadmin as well as from postgres console ?

like image 953
MANU Avatar asked Oct 06 '16 07:10

MANU


People also ask

How do I copy a table from one schema to another?

In SQL Management studio right click the database that has the source table, select Tasks -> Export data. You will be able to set source and destination server and schema, select the tables you wish to copy and you can have the destination schema create the tables that will be exported.


2 Answers

You can use create table ... like

create table schema2.the_table (like schema1.the_table including all); 

Then insert the data from the source to the destination:

insert into schema2.the_table select *  from schema1.the_table; 
like image 163
a_horse_with_no_name Avatar answered Sep 24 '22 03:09

a_horse_with_no_name


You can use CREATE TABLE AS SELECT. This ways you do not need to insert. Table will be created with data.

CREATE TABLE schema2.the_table AS  SELECT * FROM schema1.the_table; 
like image 21
Alec Avatar answered Sep 23 '22 03:09

Alec