Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump a table without sequence table in postgres

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?

like image 721
Rafiu Avatar asked Nov 22 '13 09:11

Rafiu


1 Answers

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.

like image 143
Daniel Vérité Avatar answered Oct 23 '22 15:10

Daniel Vérité