0

I want to copy data from Table A to Table B in Postgres. Table A contains 40 columns and Table B contains 20 columns. It's like Table B is the subset of Table A, which means - Table B contains only some columns which are in Table A.

I have found the answer https://stackoverflow.com/a/7483174/12556735 for copying the data if there are less number of columns.

Since there are many columns , is there any way in which we can copy data without mentioning the Column names?

Bhanu Prakash
  • 149
  • 3
  • 13

1 Answers1

0

Choice 1: If you notice, your referred question / answer itself answers your question as does not have any limitation on the number of columns. But you should know the original column definition and the required column list. It can run well for many columns.

Choice 2: If the columns are unknown, you shall try using

create table 'newtable' as (select * from 'existingtable');

Choice 3: If the columns are unknown and you wanted to create a new table with selected columns ! (which means you should know about columns), you shall try

select * from information_schema.columns where table_schema= 'yourdatabase' and table_name= 'yourtable';

And among the columns list, you shall use column_name, column_type, is_nullable, etc can be used on your script.