从pg_dump中排除序列

7
我正在创建一个排除了某些表的postgres数据库(10.1)的导出版。我遵循了如何在pg_dump中排除特定序列中的说明。然而,被排除的表的序列仍然被包括在内。有没有办法确保它们不被包含?
为了确定问题,我创建了一个名为“include”和“exclude”的小样本数据库,向每个表添加了一行,并使用以下命令导出了数据库:
pg_dump --host=localhost --no-owner --no-acl --verbose --schema=public --dbname=export_test --exclude-table=exclude --file=exclude_table_only.dump

转储文件中没有包括“exclude”表,但它包括序列:
...
--
-- TOC entry 198 (class 1259 OID 3818320)
-- Name: exclude_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE exclude_id_seq
...

我认为这 通常 是不可能的。您将不得不编辑 .sql 脚本,手动或使用 sed 进行编辑,或者将脚本提交到一个暂存数据库中,在那里删除序列(使用目录自动化脚本)并再次将临时数据库转储到新脚本中。 - joop
3个回答

9
您可以使用另一个--exclude-table来明确地排除该序列:
--exclude-table=exclude_id_seq

最终效果应该是这样的:

$ pg_dump --host=localhost --no-owner --no-acl --verbose --schema=public --dbname=export_test --exclude-table=exclude --exclude-table=exclude_id_seq --file=exclude_table_only.dump

1
不必添加多个 --exclude-table,您可以尝试使用 --exclude-table=<schema>.* 来排除该特定 schema 中的所有表。 - chintan thakar

7

排除表格,

--exclude-table

将起作用。

要排除某些序列,

--exclude-table-data 

测试环境为Postgres 12

我无法使用--exclude-table排除序列即表数据。

以下是排除序列进行导出的示例:

$ pg_dump -v -C -Fp -O -x -h employee.us-east-1.rds.amazonaws.com \ 
          -U user1 -d emp -n empschema \ 
          --exclude-table="empschema.employee_history_id_seq" \ 
          -f dump-test.sql

where,
-v : verbose
-C : create Database commands in dump
-Fp : output file in plain Text
-O : no owner details in dump
-x : no privileges details in dump
-h : hostname
-U : username
-d : database
-n : schema
--exclude-table-data : excluding the sequence
-f : file to be written into

请如有不同意见留言。
请参考:https://www.postgresql.org/docs/12/app-pgdump.html

澄清一下,例如:--exclude-table-data=my_sequence - Peter Lamberg

-1

下面的pg_dump.exe命令将为给定数据库提供表的独占模式。 pg_dump.exe -h localhost -d database -W -U username -p 5XX2 -T m_19* -T m_200* -T m_201* -f dbsave.sql

其中被排除的表是m_19*,m_200*和m_201等。


问题并不涉及排除表格。 - mustaccio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接