简体   繁体   中英

Django + Postgres: Trying dump and restore database, but are seeing ERROR: relation “*_id_seq” does not exist for all sequence tables

I am trying to move a database from a virtual machine (docker-machine) over to a database server on azure. I am first using the following command to dump the database to a local file:

pg_dump -h <virtual-machine-ip> -U <username> postgres > dump.sql

Then I try to restore it on the new server:

psql -h <database-server-ip> -U <username> -d <new_database_name> -f dump.sql

Which produces a lot of errors (example below):

SET
SET
SET
SET
SET
SET
SET
SET
COMMENT
CREATE EXTENSION
COMMENT
SET
SET
SET
CREATE TABLE
ALTER TABLE
psql:dump.sql:66: ERROR:  syntax error at or near "AS"
LINE 2:     AS integer
            ^
psql:dump.sql:69: ERROR:  relation "auth_group_id_seq" does not exist
psql:dump.sql:75: ERROR:  relation "auth_group_id_seq" does not exist
CREATE TABLE
ALTER TABLE
psql:dump.sql:101: ERROR:  syntax error at or near "AS"
LINE 2:     AS integer
            ^
psql:dump.sql:104: ERROR:  relation "auth_group_permissions_id_seq" does not exist
psql:dump.sql:110: ERROR:  relation "auth_group_permissions_id_seq" does not exist
CREATE TABLE
ALTER TABLE
psql:dump.sql:137: ERROR:  syntax error at or near "AS"
LINE 2:     AS integer
            ^
psql:dump.sql:140: ERROR:  relation "auth_permission_id_seq" does not exist
psql:dump.sql:146: ERROR:  relation "auth_permission_id_seq" does not exist
CREATE TABLE
ALTER TABLE
psql:dump.sql:175: ERROR:  syntax error at or near "AS"
LINE 2:     AS integer
            ^
psql:dump.sql:178: ERROR:  relation "clients_client_id_seq" does not exist
psql:dump.sql:184: ERROR:  relation "clients_client_id_seq" does not exist
CREATE TABLE
ALTER TABLE
psql:dump.sql:214: ERROR:  syntax error at or near "AS"
LINE 2:     AS integer

I have tried reading the docs on pg_dump, but whatever I do, I get the same result...

Any idea of what is happening here? Have I missed some options that should have been included in the dump command?

Thank you very much!

As Valdemar stated, the AS integer was introduced in 10.x, and is not backwards-compatible. If you are able to regenerate the dump through the command-line interface (pg_dump), here are the steps I would advise :

  • Dump the database in plain-text format using the -Fp (or --format=plain)
  • Edit the generated file to get rid of the AS integer statements
  • Import the altered file in your destination database

TL;DR

pg_dump -h <virtual-machine-ip> -U <username> -Fp postgres > dump.sql
sed 's/AS integer//' dump.sql > altered_dump.sql
psql -h <database-server-ip> -U <username> -d <new_database_name> -f altered_dump.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM