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

Ste*_*ett 1 postgresql pg-dump database-dump

我有一个脚本,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
Run Code Online (Sandbox Code Playgroud)

这导致整个转储中止。

有没有一种方法可以:

  • 忽略用户不拥有的表?
  • 忽略错误?

我真的不希望这些表出现在转储中,因此即使我们可以访问它们,也无法完全解决问题。

(Postgres 9.6.3)

Ste*_*ett 5

似乎没有标准的方法可以执行此操作,但是使用该--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';" )
Run Code Online (Sandbox Code Playgroud)

这将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 
Run Code Online (Sandbox Code Playgroud)