0

Context

I have a PostgreSQL database structure with a table1 defined with many project specific fields (which are empty now, but which will get written through an application):

table1

I have another table, let's say table3, already populated with (many) records of base data:

table3

I'd like to append, in a column-wise manner, all the columns from table3 into table1.
These columns do not exists yet in table1.

The desired results would by as follows:

table3 columns appended into table1

Question

How to achieve this in a nice way?

What I've tried so far

I have tried to follow this, but without success because the columns defined in table3 do not exist in table1:

UPDATE table1 a
SET (fid, attrib1, attrib2, attrib3, ...)
    =
    (b.fid, b.attrib1, b.attrib2, b.attrib3, ...)
FROM   table3 b
;

I also tried using an `INSERT` statement:
INSERT INTO table1
SELECT fid, attrib1, attrib2, attrib3, ...
FROM table3;

But here I face this (obvious error):

ERROR: INSERT has more expressions than target columns

The third way I was thinking about was to ALTER table1 and add all new columns, one by one, which would be tedious because of the datatype definition (which I don't want to care about because it's already defined in table3). There is probably a smarter way of doing that.

s.k
  • 414
  • 1
  • 9
  • 26

0 Answers0