gpt4 book ai didi

sql - pg_dump 与 COPY (SELECT * FROM my_table)

转载 作者:行者123 更新时间:2023-12-04 20:42:40 25 4
gpt4 key购买 nike

我需要将表的内容从一个数据库复制到另一个具有相同表的数据库,该表当前为空。

我打算转储旧表中的表数据,然后简单地将其导入到新数据库中的空表中。但是,我在使用 pg_dump 时遇到了一些我不理解的行为。

我尝试使用以下命令将表数据转储到文件中:

pg_dump -a -t '"my_table"' my_database > /tmp/my_table.sql

这有效,但如果我像这样查看表,我只会得到 8 条记录,并且表中有 1000 多条记录:
SELECT * FROM my_table;

所以,我尝试使用 COPY 命令生成一个 .csv 文件,我看到了类似的行为:
COPY my_table TO '/tmp/my_table.csv' WITH CSV HEADER;

我得到与 pg_dump 相同的 8 条记录。但是,与:
COPY (SELECT * FROM my_table) TO '/tmp/my_table.csv' WITH CSV HEADER;

我得到了所有 1266 条记录。

我认为这些命令都应该返回相同的数据,但显然,我错了。有什么不同?

最佳答案

my_table有没有可能是继承层次结构的一部分吗?我问是因为 http://www.postgresql.org/docs/9.0/interactive/sql-copy.html#AEN58984有这个:

COPY only deals with the specific table named; it does not copy data to or from child tables. Thus for example COPY table TO shows the same data as SELECT * FROM ONLY table. But COPY (SELECT * FROM table) TO ... can be used to dump all of the data in an inheritance hierarchy.



您应该能够通过运行来检查:
SELECT * FROM ONLY my_table;

如果那只返回 8 条记录,那么我们就在正确的轨道上,我们只需要找到子表( How to find child tables that inherit from another table in PSQL 会有所帮助)。

如果没有,那么我不确定 - 我想知道规则或触发器是否参与其中,但我目前看不到如何。不过,也许它给了别人一个想法......?

关于sql - pg_dump 与 COPY (SELECT * FROM my_table),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252903/

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