SQL 导出数据库的 CREATE 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6024108/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:34:52  来源:igfitidea点击:

Export a CREATE script for a database

sqldatabasepostgresql

提问by serv-bot 22

Say I've created a database in pgAdmin, but I want to export a CREATE sql file.

假设我在 pgAdmin 中创建了一个数据库,但我想导出一个 CREATE sql 文件。

How would I go about generating the dump?

我将如何生成转储?

采纳答案by unmounted

To generate a sql script that will create the tables as they exist in a given database do:

要生成一个 sql 脚本来创建给定数据库中存在的表,请执行以下操作:

pg_dump --schema-only --no-owner the_database > create_the_tables.sql

pg_dump --schema-only --no-owner the_database > create_the_tables.sql

This will give you a bunch of create table statements. Just to see how portable it was I tried the above as follows:

这会给你一堆创建表语句。只是为了看看它有多便携,我尝试了以下方法:

bvm$ pg_dump -s --no-owner devdb | sqlite3 so_ans.db

And then:

进而:

bvm$ sqlite3 so_ans.db .schema
CREATE TABLE courses (
    id integer NOT NULL,
    name text,
    created_by integer,
    jc text
);

Kind of cool.

有点酷。

回答by Nate Cook

Here's how to use pgAdmin to create a schema script that can be used with a PostgreSql database schema comparison tool such as apgdiff. These instructions are for pgAdmin3.

以下是如何使用 pgAdmin 创建可与 PostgreSql 数据库模式比较工具(例如apgdiff )一起使用的模式脚本。这些说明适用于 pgAdmin3。

  1. In pgAdmin, right click on the database and click Backup.
  2. Enter an appropriate path and filename (i.e. /some/path/my_script.sql).
  3. Select Plain as the format in the format dropdown.
  4. Go to the Dump Options #1tab and check "Only schema".
  5. Then click Backup. Then click Done.
  1. 在 pgAdmin 中,右键单击数据库并单击备份。
  2. 输入适当的路径和文件名(即/some/path/my_script.sql)。
  3. 在格式下拉列表中选择普通作为格式。
  4. 转到转储选项 #1选项卡并选中“仅模式”。
  5. 然后单击备份。然后单击完成。

Note:Yes, I realize that pgAdmin uses pg_dump behind the scenes to create the script, but the question was about pgAdmin, so this is the GUI method.

注意:是的,我意识到 pgAdmin 在幕后使用 pg_dump 来创建脚本,但问题是关于 pgAdmin,所以这是 GUI 方法。

回答by Tanveer Ali

pgAdmin however does have a facility to do what you want:

然而,pgAdmin 确实可以做你想做的事:

Right-click on the database you want to export

右键单击要导出的数据库

Select Backup from the pop-up menu

从弹出菜单中选择备份

Choose "format" Plain.

选择“格式”普通。

Choose "plain option" Only schema

选择“普通选项”仅模式

回答by Anurag

You can achieve this through phpPgAdmin just like phpMyAdmin for MySQL.

您可以通过 phpPgAdmin 实现这一点,就像 MySQL 的 phpMyAdmin 一样。

Login to phpPgAdmin select the database and then choose export.

登录 phpPgAdmin 选择数据库,然后选择导出。

回答by fresheed

At least in PgAdmin III 1.22.1 you can get CREATE script doing: 1) right-click on table name 2) "Scripts" -> "CREATE script" There are options to get SELECT, DELETE etc.

至少在 PgAdmin III 1.22.1 中,您可以获得 CREATE 脚本:1)右键单击表名 2)“脚本”->“CREATE 脚本” 有获取 SELECT、DELETE 等选项的选项。