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?
6 回答
Use --table
to tell pg_dump
what table it has to backup:
pg_dump --host localhost --port 5432 --username postgres --format plain --verbose --file "<abstract_file_path>" --table public.tablename dbname
如果你在 Ubuntu 上,
- 登录到您的 postgres 用户
sudo su postgres
pg_dump -d <database_name> -t <table_name> > file.sql
确保您正在执行postgres
用户具有写入权限的命令(示例/tmp
:)
编辑
如果您想将 .sql 转储到另一台计算机中,您可能需要考虑跳过保存到 .sql 文件中的所有者信息。
您可以使用pg_dump --no-owner -d <database_name> -t <table_name> > file.sql
pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table > backup.sql
您可以备份单个表,但我建议备份整个数据库,然后恢复您需要的任何表。备份整个数据库总是好的。
作为对 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
请注意,我省略了--ignore-version
标志,因为它已被弃用。
你可以使用这个命令
pg_dump --table=yourTable --data-only --column-inserts yourDataBase > file.sql
你应该改变你的表,你的数据库为你的情况