生成脚本以删除和创建数据库中所有对象的最简单方法是什么?

What is the easiest way to generate a script to drop and create all objects in a database?

我习惯于使用 SQL 服务器,SQL 服务器管理工​​作室可以选择自动生成脚本来删除和重新创建数据库中的所有内容 (tables/views/procedures/etc ).我发现,当开发一个新的应用程序并在本地数据库中编写一堆垃圾以进行基本测试时,拥有对整个事物进行核对并在干净的平板上重新创建它的选项非常有帮助,所以我正在寻找类似的postgres/pgadmin.

内的功能

PGAdmin 可以选择为特定 table 生成创建脚本,但右键单击每个 table 会非常乏味,我想知道是否有其他方法可以做到这一点。

clean 飞路

database migration tool Flyway offers a clean 命令删除已配置架构中的所有对象。

引用文档:

Clean is a great help in development and test. It will effectively give you a fresh start, by wiping your configured schemas completely clean. All objects (tables, views, procedures, …) will be dropped.

Needless to say: do not use against your production DB!

要重新创建一个干净的仅模式数据库,您可以使用 Postgres 服务器安装中包含的 pg_dump 客户端。使用的选项是:

-c

--clean

Output commands to clean (drop) database objects prior to outputting the commands for creating them. (Unless --if-exists is also specified, restore might generate some harmless error messages, if any objects were not present in the destination database.)

This option is ignored when emitting an archive (non-text) output file. For the archive formats, you can specify the option when you call pg_restore.

和:

-s

--schema-only

Dump only the object definitions (schema), not data.

This option is the inverse of --data-only. It is similar to, but for historical reasons not identical to, specifying --section=pre-data --section=post-data.

(Do not confuse this with the --schema option, which uses the word “schema” in a different meaning.)

To exclude table data for only a subset of tables in the database, see --exclude-table-data.