5

I have a table named test with id column. And id column has updated by sequence called "test_id_seq". This test table was created by the user "A" and another user "B" have the read permission. While creating the dump for table from the user "B". Using the following comment

pg_dump -U B -t test rafiu > test.sql

it showing the error like

pg_dump: [archiver (db)] query failed: ERROR: permission denied for relation test_id_seq

Is there any option to dump only the table?

4
  • What kind of dump do you really require? If you require data that is accessible also for user B, use --data-only option. Commented Nov 22, 2013 at 9:47
  • I need table structure and data. Is that possible. Commented Nov 22, 2013 at 9:51
  • The sequence counts as part of the structure. Any reason not to grant "B" permission to dump the sequence? Commented Nov 22, 2013 at 11:06
  • I tested it on the same case and it works. Just take care if you have both schema and sequence in the same schema or if you don't have another security restriction. Grant use to connect a database and select a table to make dump is enough. Commented Nov 22, 2013 at 11:13

2 Answers 2

7

When the sequence is "owned" by a column, it's dumped with it, so the user producing the dump must have the rights to access it (GRANT SELECT ON sequence_name TO username)

This happens when using the SERIAL/BIGSERIAL pseudo-datatypes. However, the sequence can still be detached from the column afterwards by issuing:

ALTER SEQUENCE test_id_seq OWNED BY none;

After that, assigning a default value for test.ID with the sequence will continue to work as usual, but pg_dump will make no attempt to dump the sequence with the table.


If the table is created from the start with a pre-existing sequence (not using SERIAL), then the outcome is the same without the need for ALTER SEQUENCE.

Example:

create sequence seq1;
create table test1 (id int default nextval('seq1'));

In this case the table would be dumped with pg_dump -t test1 as:

CREATE TABLE test1 (
    id integer DEFAULT nextval('seq1'::regclass)
);

with no other reference to seq1 and no need for permission to read it.

Sign up to request clarification or add additional context in comments.

1 Comment

So I guess the answer is no, there is no way to use pg_dump on a table that was created with the serial pseudo-datatype, if you don't have permissions on the sequence, without getting the admin to modify the sequence ownership. This is unfortunate. I suppose in my case I'll manually extract the data and import it again using the json functions.
5

Have you tried using the -T option to neglect the sequence?

pg_dump -U B -T test_id_seq rafiu > test.sql

This should take a pgdump without that sequence.

3 Comments

I have, and it doesn't work. (pg_dump from PostgreSQL 9.4)
this should not be the accepted answer. it doesn't work.
The sequence query is in the test table referencing the sequence table, so the privileges will still prevent the dump

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.