使用pg_dump转储数据库,忽略我们无法访问的表。

5
我有一个脚本,其中 pg_dump 出现错误并显示以下消息:
pg_dump -h db1 --format plain --encoding UTF8 --schema=public --schema-only --no-owner me
pg_dump: [archiver (db)] query failed: ERROR:  permission denied for relation notmytable
pg_dump: [archiver (db)] query was: LOCK TABLE public.notmytable IN ACCESS SHARE MODE

这会导致整个转储过程中止。

是否有以下两种方法可供选择:

  • 忽略非我们用户拥有的表?
  • 忽略错误?

我真的不想在转储过程中包含这些表,所以即使我们能访问它们,也不能完全解决问题。

(Postgres 9.6.3)

1个回答

10

目前似乎没有标准的方法来实现此功能,但是通过使用--exclude-table标志,我们可以使用一个解决方法:

export EXCLUDETABLE=$(psql -t -h $HOST -d $DBNAME -c "select '--exclude-table=' || string_agg(tablename,' --exclude-table=') FROM pg_catalog.pg_tables WHERE tableowner NOT LIKE 'myuser';" )

这将使EXCLUDETABLE看起来像--exclude-table=foo --exclude-table=blah
现在我们将其传递给pg_dump:
echo Excluding these tables from dump: $EXCLUDETABLE
pg_dump -h $HOST --format plain --encoding UTF8 --schema=public --schema-only --no-owner $EXCLUDETABLE $DBNAME > public-schema.sql 

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