PostgreSQL 数据库设计的备份 - 无数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43228951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backup of PostgreSQL database design - without data
提问by Helge
I have tried to find a way to backup my database design without including the data stored in the database. Actually, one might say that I want to have a file that holds all "CREATE Scripts" in the database. Ideally, this file could be used to recreate the database (with no data of course).
我试图找到一种方法来备份我的数据库设计,而不包括存储在数据库中的数据。实际上,有人可能会说我想要一个包含数据库中所有“CREATE Scripts”的文件。理想情况下,此文件可用于重新创建数据库(当然没有数据)。
How to avoid data in a database backup?
如何避免数据库备份中的数据?
I am using pgAdmin 4.1.3 and PostgreSQL 9.6.
我正在使用 pgAdmin 4.1.3 和 PostgreSQL 9.6。
回答by z44.nelther
You can use this from psql(terminal):
您可以从 psql(terminal) 使用它:
pg_dump -s databasename > file.dump
from pg_dump documentation the "-s" dump only the object definitions (schema), not data.
从 pg_dump 文档中,“-s”仅转储对象定义(架构),而不转储数据。
回答by Md Ayub Ali Sarker
pg_dump --host localhost --port 5432 --username "userName" --schema-only --verbose --file "file path" "db_dev_local"
回答by isapir
The pg_dump
utility is distributed with Postgres in the bin
directory. The utility accepts many options as documented, and the format is:
该pg_dump
实用程序与bin
目录中的Postgres 一起分发。该实用程序接受许多文档中的选项,格式为:
pg_dump <options> <database-name>
To answer specifically the question here, open a Command Prompt window (Shell in *nix), and navigate to the Postgres bin
directory. Then issue the following command (Windows syntax shown):
要具体回答此处的问题,请打开命令提示符窗口(*nix 中的 Shell),然后导航到 Postgresbin
目录。然后发出以下命令(显示的是 Windows 语法):
> set DB_NAME=mydatabase
> pg_dump.exe --username=postgres --schema-only %DB_NAME% >%DB_NAME%-schema.sql
The DB_NAME
environment variable is purely for convenience, and the name can be inlined for simplicity.
的DB_NAME
环境变量纯粹是为了方便,并且名称可以被内联为简单起见。
The --username
switch is required in most cases, as you are probably not logged in to your OS as a valid Postgres user.
该--username
交换机需要在大多数情况下,你很可能没有登录到您的操作系统作为一个有效的Postgres用户。
The --schema-only
switch indicates that you only want the DDL statements, without the actual data.
该--schema-only
开关表示您只需要 DDL 语句,而不需要实际数据。
The >%DATABASE_NAME%-schema.sql
portion redirects the output from the console to a file named %DATABASE_NAME%-schema.sql
, in this example "mydatabase-schema.sql".
该>%DATABASE_NAME%-schema.sql
部分将控制台的输出重定向到名为 的文件%DATABASE_NAME%-schema.sql
,在本例中为“mydatabase-schema.sql”。
The produced file is a valid SQL script, which can be executed via any valid client in order to recreate all the objects.
生成的文件是一个有效的 SQL 脚本,它可以通过任何有效的客户端执行以重新创建所有对象。
Here are a couple of other useful examples:
以下是一些其他有用的示例:
I usually prefix MATERIALIZED VIEWs with mv_
, so to dump the definitions for only the relations that start with "mv_*" I use:
我通常在 MATERIALIZED mv_
VIEW 前面加上,所以为了只转储以“mv_*”开头的关系的定义,我使用:
> pg_dump --username=postgres --schema-only --table=mv_* %DB_NAME% >%DB_NAME%-matviews.sql
Use multiple --table=
arguments for multiple patterns.
--table=
对多个模式使用多个参数。
To script only the POST-DATA objects, e.g. indexes, triggers, etc., use --section=post-data
要仅编写 POST-DATA 对象(例如索引、触发器等)的脚本,请使用 --section=post-data
> pg_dump --username=postgres --section=post-data %DB_NAME% >%DB_NAME%-post-data.sql
This assumes default host, port, authentication, etc. To specify non-default settings refer to the documentation
这假定默认主机、端口、身份验证等。要指定非默认设置,请参阅文档