gpt4 book ai didi

postgresql - psql clean/reset database to just schema

转载 作者:行者123 更新时间:2023-11-29 12:49:15 25 4
gpt4 key购买 nike

我对我的数据库进行了导入,但它带有内容。我想将数据库重置为仅模式。这意味着表中的所有内容都已删除,但保留了表的架构。自动递增字段被重置为从头开始。

这可以用 psql 实现吗?我看到使用特定表命令删除内容的命令,我只想要一个通用的 psql reset-to-schema

最佳答案

转储您的数据库(使用 pg_dump),删除并重新创建数据库(您现在有一个空数据库)并恢复所有模式数据(使用 pg_restore 和选项 --schema-only)。这只会恢复数据定义(表、类型、序列、函数等),而不是数据本身。

有关更多选项,请查看手册。如果根据需要设置了所有 PG 环境变量,您可能会使用这些行,否则您至少需要指定一些连接信息:

pg_dump -F c -f <dump_file_name_here> <db_name_here>
pg_restore -c -C -F c -d <db_name_here> --schema-only <dump_file_name_here>

也可以将选项--schema-only 放入pg_dump。如果您不想再使用这些数据,这可能会大大加快速度。

关于postgresql - psql clean/reset database to just schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157996/

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