postgress将数据从数据库导入到另一个具有更多列表的数据库

时间:2019-03-29 15:31:47

标签: postgresql

我在不同的机器上有两个postgres 9.6数据库:

  • db1-包含我需要的数据的旧版本
  • db2-模式已更新,现在它包含更多列。我只在这里使用架构。我不需要数据

我想将所有数据从db1带到具有较新模式的db2。有很多表,我不想手动更改它们。

我将如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用复制命令,将列名指定为copy table_name(column ...)到....,并从...复制table_name(column ...)

复制到导出并从复制到导入 https://www.postgresql.org/docs/10/sql-copy.html

COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
TO { 'filename' | STDOUT }
[ [ WITH ]
      [ BINARY ]
      [ OIDS ]
      [ DELIMITER [ AS ] 'delimiter' ]
      [ NULL [ AS ] 'null string' ]
      [ CSV [ HEADER ]
            [ QUOTE [ AS ] 'quote' ]
            [ ESCAPE [ AS ] 'escape' ]
            [ FORCE QUOTE { column_name [, ...] | * } ] ] ]

COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | STDIN }
[ [ WITH ]
      [ BINARY ]
      [ OIDS ]
      [ DELIMITER [ AS ] 'delimiter' ]
      [ NULL [ AS ] 'null string' ]
      [ CSV [ HEADER ]
            [ QUOTE [ AS ] 'quote' ]
            [ ESCAPE [ AS ] 'escape' ]
            [ FORCE NOT NULL column_name [, ...] ] ] ]