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
How to create a backup of a single table in a postgres database?
提问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 --table
to tell pg_dump
what 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,
- Login to your postgres user
sudo su postgres
pg_dump -d <database_name> -t <table_name> > file.sql
- 登录到您的 postgres 用户
sudo su postgres
pg_dump -d <database_name> -t <table_name> > file.sql
Make sure that you are executing the command where the postgres
user 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.
您可以备份单个表,但我建议备份整个数据库,然后恢复您需要的任何表。备份整个数据库总是好的。
回答by Franck Dernoncourt
回答by user3207874
As an addition to Frank Heiken's answer, if you wish to use INSERT
statements instead of copy from stdin
, then you should specify the --inserts
flag
作为 Frank Heiken 答案的补充,如果您希望使用INSERT
statements 而不是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-version
flag, because it is deprecated.
请注意,我省略了该--ignore-version
标志,因为它已被弃用。