如何从 postgresql 打印表结构?

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

How to print the table structure from postgresql?

postgresqlprintingphppgadminpgadmin

提问by svk

I am using phpPgAdmin in the browser and PgAdmin III for Windows. Is there anyway to take the printout of the table structure for the entire database?

我在浏览器中使用 phpPgAdmin,在 Windows 中使用 PgAdmin III。有没有办法打印整个数据库的表结构?

回答by filiprem

The standard way of exporting database schema is pg_dump:

导出数据库模式的标准方法是pg_dump

#!/bin/sh
pg_dump --schema-only MYDBNAME > output-file.sql

Sligtly better way combines pg_dump with pg_restorelist filtering:

稍微更好的方法是将 pg_dump 与pg_restore列表过滤结合起来:

#!/bin/sh
dump=`mktemp`
list=`mktemp`
pg_dump --schema-only MYDBNAME -Fc -f $dump
pg_restore -l $dump | grep ' TABLE ' > $list
pg_restore -L $list $dump > output-file.sql
rm $list $dump

If you prefer GUI wizards, pg_dump command can be generated in PgAdmin III:

如果您更喜欢 GUI 向导,可以在PgAdmin III 中生成 pg_dump 命令:

  • right click the database in object browser, select "Backup"
  • select destination filename (common extension is .sql or .txt)
  • Choose "Plain" format. (that is, text format)
  • on "Dump Options #1" tab, tick "Only Schema"
  • click "Backup"
  • 在对象浏览器中右键单击数据库,选择“备份”
  • 选择目标文件名(常用扩展名是 .sql 或 .txt)
  • 选择“普通”格式。(即文本格式)
  • 在“Dump Options #1”选项卡上,勾选“Only Schema”
  • 点击“备份”

Note: the resulting file will have not only tables, but also all other objects (views, functions, etc.). If you need only the minimal printout, you can edit this file in text editor and remove unneeded stuff. Leave only "Type: TABLE;" items.

注意:生成的文件不仅有表格,还有所有其他对象(视图、函数等)。如果您只需要最少的打印输出,您可以在文本编辑器中编辑此文件并删除不需要的内容。只留下“类型:表格;” 项目。

回答by omikron

If you are on Windows and pgAdmin, you should have psql somewhere in C:\Program files\postgresql\<version>\bin\psql.

如果你使用的是 Windows 和 pgAdmin,你应该在C:\Program files\postgresql\<version>\bin\psql.

Run psql and then you have \dwhich prints all tables and indexes and \d <table_name>which gives you details about one table.

运行 psql 然后你就\d可以打印所有表和索引,\d <table_name>并为你提供有关一张表的详细信息。

回答by sniperwolf897

You can do them one at a time as you need them. Right click on a table in pgAdminIII, go to Reports and select "Data Dictionary Report".

您可以根据需要一次完成一项。右键单击 pgAdminIII 中的表,转到报告并选择“数据字典报告”。

For the output format, select "XHTML 1.0 Transitional", choose "Embed the default stylesheet" option, give it a file name and click OK.

对于输出格式,选择“XHTML 1.0 Transitional”,选择“嵌入默认样式表”选项,给它一个文件名,然后单击“确定”。

Open the XML file in your browser and print.

在浏览器中打开 XML 文件并打印。