postgresql 如何在 postgres 数据库中创建单个表的备份?

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

How to create a backup of a single table in a postgres database?

postgresqlbackuppg-dump

提问by Elitmiar

Is there a way to create a backup of a single table within a database using postgres? And how? Does this also work with the pg_dump command?

有没有办法使用 postgres 在数据库中创建单个表的备份?如何?这也适用于 pg_dump 命令吗?

回答by Frank Heikens

Use --tableto tell pg_dumpwhat table it has to backup:

使用--table告诉pg_dump什么表它必须备份:

pg_dump --host localhost --port 5432 --username postgres --format plain --ignore-version --verbose --file "<abstract_file_path>" --table public.tablename dbname

回答by Sri Harsha Kappala

If you are on Ubuntu,

如果您使用的是 Ubuntu,

  1. Login to your postgres user sudo su postgres
  2. pg_dump -d <database_name> -t <table_name> > file.sql
  1. 登录到您的 postgres 用户 sudo su postgres
  2. pg_dump -d <database_name> -t <table_name> > file.sql

Make sure that you are executing the command where the postgresuser have write permissions (Example: /tmp)

请确保您正在执行的命令,其中的postgres用户具有写权限(例如:/tmp

Edit

编辑

If you want to dump the .sql in another computer, you may need to consider skipping the owner information getting saved into the .sql file.

如果您想将 .sql 转储到另一台计算机中,您可能需要考虑跳过保存到 .sql 文件中的所有者信息。

You can use pg_dump --no-owner -d <database_name> -t <table_name> > file.sql

您可以使用 pg_dump --no-owner -d <database_name> -t <table_name> > file.sql

回答by Prashant Kumar

pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table > backup.sql

pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table > backup.sql

You can take the backup of a single table but I would suggest to take the backup of whole database and then restore whichever table you need. It is always good to have backup of whole database.

您可以备份单个表,但我建议备份整个数据库,然后恢复您需要的任何表。备份整个数据库总是好的。

9 ways to use pg_dump

9 种使用 pg_dump 的方法

回答by Franck Dernoncourt

If you prefer a graphical user interface, you can use pgAdmin III (Linux/Windows/OS X). Simply right click on the table of your choice, then "backup". It will create a pg_dumpcommand for you.

如果您更喜欢图形用户界面,则可以使用 pgAdmin III (Linux/Windows/OS X)。只需右键单击您选择的表,然后“备份”。它将pg_dump为您创建一个命令。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by user3207874

As an addition to Frank Heiken's answer, if you wish to use INSERTstatements instead of copy from stdin, then you should specify the --insertsflag

作为 Frank Heiken 答案的补充,如果您希望使用INSERTstatements 而不是copy from stdin,那么您应该指定--inserts标志

pg_dump --host localhost --port 5432 --username postgres --format plain --verbose --file "<abstract_file_path>" --table public.tablename --inserts dbname

pg_dump --host localhost --port 5432 --username postgres --format plain --verbose --file "<abstract_file_path>" --table public.tablename --inserts dbname

Notice that I left out the --ignore-versionflag, because it is deprecated.

请注意,我省略了该--ignore-version标志,因为它已被弃用。