pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Sherrylyn Branchaw <sbranchaw@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: pg_restore restores privileges differently from psql
Date: Mon, 14 Jan 2019 11:11:58 -0800
Message-ID: <95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com> (raw)
In-Reply-To: <CAB_myF5_+Ok=VRkHPyxJ_TfgrG8AHdNjQs2ke5B+hKvTCzmmPQ@mail.gmail.com>
References: <CAB_myF5_+Ok=VRkHPyxJ_TfgrG8AHdNjQs2ke5B+hKvTCzmmPQ@mail.gmail.com>

On 1/14/19 10:15 AM, Sherrylyn Branchaw wrote:
> Hi,
> 
> I'm running two Postgres 9.6.11 databases on RHEL 6.9.
> 
> I'm restoring the schema from one database (prod) to another (dev). 
> There are users that exist in prod that don't exist in dev. When the 
> restore job tries to grant privileges to nonexistent users in dev, I 
> would like it to generate an error, which is safe to ignore, but still 
> correctly grant privileges to any user that does exist in dev.
> 
> That's the behavior I see when I dump to a plain file and restore it 
> using /psql -f/, but not the behavior I see when I do a /pg_dump 
> -Fc/ followed by /pg_restore./ /pg_restore /seems to treat all the 
> /GRANT/ statements for a single object as a single statement, and when 
> one errors out, they all error out, meaning I'm left with no privileges 
> on the object in question.
> 
> For instance, when this appears in my plaintext file:
> GRANT ALL ON SCHEMA test TO user1;
> GRANT USAGE ON SCHEMA test TO user2;
> GRANT USAGE ON SCHEMA test TO user3;
> 
> and user1 doesn't exist on the target database, user2 and user3 get the 
> expected privileges when restoring from /psql/ but not from /pg_restore/.
> 
> Here's a reproducible test case.
> 
> CREATE DATABASE prod_db;
> CREATE DATABASE dev_db_pg_restore;
> CREATE DATABASE dev_db_psql;
> CREATE ROLE prod_user;
> CREATE ROLE prod_and_dev_user;
> 
> -- in prod_db
> CREATE SCHEMA test;
> GRANT ALL ON SCHEMA test TO prod_user;
> GRANT USAGE ON SCHEMA test TO prod_and_dev_user;
> 
> pg_dump -Fc prod_db &> prod_dump.bin
> pg_dump prod_db &> prod_dump.sql
> 
> -- On database instance containing the dev dbs.
> DROP ROLE prod_user;

The above needs more information:

1) Are the dev_* databases on a different cluster?

2) If so did you run:

CREATE ROLE prod_user;
CREATE ROLE prod_and_dev_user;

on that cluster first?

Also if so:

In the restores below are you sure you are pointed at the same cluster 
in each case?

> 
> pg_restore prod_dump.bin -d dev_db_pg_restore
> psql -d dev_db_psql -f prod_dump.sqlu 

What do you see if you do:

pg_restore -f prod_dump_restore.sql prod_dump.bin

and look in prod_dump_res?tore.sql?

> 
> -- In dev_db_psql
> SELECT has_schema_privilege('prod_and_dev_user', 'test', 'usage');
> Expected result: true
> Actual result: true
> 
> -- In dev_db_pg_restore
> SELECT has_schema_privilege('prod_and_dev_user', 'test', 'usage');
> Expected result: true
> Actual result: false
> 
> The behavior seems to be related to the fact that /pg_restore/ reports 
> the failed command as containing all the semicolon-delimited privilege 
> command, which get executed separately when restoring from plaintext:
> 
> pg_restore: [archiver (db)] could not execute query: ERROR:  role 
> "prod_user" does not exist
>      Command was: GRANT ALL ON SCHEMA test TO prod_user;
> GRANT USAGE ON SCHEMA test TO prod_and_dev_user;
> 
> As a workaround, I've created a unprivileged dummy user by this name on 
> the dev database, but my question is, is this a bug or feature? If a 
> feature, is the behavior documented? I didn't find any documentation, 
> but that doesn't mean it doesn't exist.
> 
> Thanks,
> Sherrylyn


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (8+ messages)  latest in thread

Message-ID: <95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com>
Permalink:  ../95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com/
Also on:    postgresql.org/message-id/95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, sbranchaw@gmail.com
  Subject: Re: pg_restore restores privileges differently from psql
  In-Reply-To: <95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com>
  References: <CAB_myF5_+Ok=VRkHPyxJ_TfgrG8AHdNjQs2ke5B+hKvTCzmmPQ@mail.gmail.com> <95556308-6e54-a5a7-b9f7-591b8a5f87d8@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox