Postgres copies data between tables

I created table1with columns a,b,c,din which there is data. table2basically the same as table1, it has different order columns + extra ie column a,e,d,b,cwithout data.

how can I copy data from table1to table2, note that the column ais idand I want the number to remain the same.

this is what i have already tried:

insert into table2 select (a,d,b,c) from table1

this led to column "a" is of type bigint but expression is of type record

insert into table2 (a,d,b,c) values(select a,d,b,c from table1)

doesn't work either syntax error at or near "select"

insert into table2 (a,e,d,b,c) values(select a,NULL,d,b,c from table1)

got an error: INSERT has more target columns than expressions

+5
source share
2 answers

Specify the column names that you insert, but do not use valueswhen defining a selection.

insert into table2(a,d,b,c) select a, d, b, c  from table1
+14
source

postgreSQL, :

INSERT INTO [Tablename]([columns]) SELECT [Columns] FROM [Table to copy form];

:

INSERT INTO table2(a,b,c,d) SELECT a,b,c,d FROM table1;

, :

CREATE TABLE [New Table] AS [Old Table] WITH NO DATA;

INSERT .

, : CREATE TABLE [New Table] as [Old Table];

, dataschool: https://dataschool.com/learn/copying-data-between-tables

: https://www.postgresql.org/docs/9.2/sql-insert.html

0

Source: https://habr.com/ru/post/1529554/


All Articles