gpt4 book ai didi

sql - 使用 PostgreSQL 在数据库之间传输数据

转载 作者:行者123 更新时间:2023-11-29 11:05:59 26 4
gpt4 key购买 nike

我需要从另一个数据库传输一些数据。旧数据库名为 paw1.movi​​esDB,新数据库为 paw1。每个表的架构如下。

Awards (name of the table)(new DB)
Id [PK] Serial Award

Nominations (name of the table) (old DB)
Id [PK] Serial nominations

如何将数据从旧数据库复制到新数据库?

最佳答案

我只需要做这件事,所以我想我应该把食谱贴在这里。这假设两个数据库都在同一台服务器上。

首先,将表从旧数据库复制到新数据库。在命令行:

pg_dump -U postgres -t <old_table> <old_database> | psql -U postgres -d <new_database>

接下来,将复制的表的权限授予新数据库的用户。登录 psql:

psql -U postgres -d <new_database>

ALTER TABLE <old_table> OWNER TO <new_user>;

\q

此时,您在新数据库中复制的表仍具有名称 <old_table>从你的旧数据库。假设您想将数据移动到其他地方,比如 <new_table> ,您可以只使用常规 SQL 查询:

INSERT INTO <new_table> (field1, field2, field3) 
SELECT field1, field2, field3 from <old_table>;

完成!

关于sql - 使用 PostgreSQL 在数据库之间传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3049864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com