pg_dump ignoring table sequence?

21,242

dumping by table only - will dump only the table. You need to dump the sequence separately in addition to the table.

If you dont know your sequence you can list it with \d yourtable in psql. You will see something in the row your sequence is on that looks like : nextval('yourtable_id_seq'::regclass')

Then from the command line, pgdump -t yourtable_id_seq

http://www.postgresql.org/docs/9.0/static/app-pgdump.html

Share:
21,242
demersus
Author by

demersus

I am a software engineer working and living in the United States. The technologies I use are primarily EmberJS, Ruby, Rails, PostgreSQL, Elasticsearch, Docker, and of course HTML, CSS, and Javascript. I have experience programming in MS technologies such as ASP.NET, C#, VB.NET, SQLServer etc. I prefer the opensource alternatives, however, because they give me more flexibility and power without vendor lock-in. My O/S of choice is Arch Linux & derivatives. I have setup and managed Linux servers for hosting websites, and office file servers. I am a Christian.

Updated on September 10, 2020

Comments

  • demersus
    demersus over 3 years

    I have been playing around with PostgreSQL lately, and am having trouble understanding how to backup and restore a single table.

    I used pgadmin3 to backup a single table in my database, in order to copy it to a different server. When I try to do a pg_restore on the file, I get error messages saying that the sequence does not exist:

    pg_restore: [archiver (db)] could not execute query: ERROR:  relation "businesses_id_seq" does not exist
        Command was: 
    CREATE TABLE businesses (
        id integer DEFAULT nextval('businesses_id_seq'::regclass) NOT NULL,
        name character varyin...
    

    It looks like the dump file did not include the sequence for my auto incrementing column. How do I get it to include that?

  • demersus
    demersus over 13 years
    Do you know if I can specify a comma after th -t flag? Like this: pgdump -t mytable, mytabl_id_seq
  • Phil Hord
    Phil Hord over 11 years
    No, but you can specify multiple -t switches. Like this: pgdump -t mytable -t mytabl_id_seq