3

I have granted priviledges to a user for testing in PostGreSql:

GRANT USAGE ON SCHEMA "MySchema" TO "Tester";
GRANT ALL ON ALL TABLES IN SCHEMA "MySchema" TO "Tester";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "MySchema" TO "Tester";

Yet, when I run the following statement;

TRUNCATE TABLE "MySchema"."USER_ACCOUNT" RESTART IDENTITY;

I get the following error message:

[error: must be owner of relation USER_ACCOUNT_ID_seq]

What else do I need to grant to my Tester user, and how? Thanks!

3
  • how are you connecting to the database? and, other command working fine such as select, delete and insert.
    – user234918
    Commented Feb 20, 2014 at 12:43
  • I am connecting via node.js' pg module. Other commands are working fine... Commented Feb 20, 2014 at 14:50
  • You might consider changing the default owner as in dba.stackexchange.com/a/295736/194832
    – Federico
    Commented Jul 16, 2021 at 1:52

1 Answer 1

4

From http://www.postgresql.org/docs/9.5/static/sql-altersequence.html

You must own the sequence to use ALTER SEQUENCE. To change a sequence's schema, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the sequence's schema. (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the sequence. However, a superuser can alter ownership of any sequence anyway.)

Restarting the sequence on table truncation is essentially the same as altering the sequence in a separate statement

TRUNCATE TABLE "MySchema"."USER_ACCOUNT";
ALTER SEQUENCE my_seq RESTART;

So, the above restriction applies to your original TRUNCATE with RESTART statement. You must be the owner of that sequence or be a super user.

You can GRANT the sequence owner's role to the role that's going to execute your statement by doing

GRANT roleA TO roleB;

assuming that roleA owns the sequence you are trying to restart.

Once you are done with all the TRUNCATE/ALTER SEQUENCE statements you can REVOKE the sequence's owning role from roleB

REVOKE roleA FROM roleB;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.