Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: constraint does not exist when restoring a backup on a fresh db #245

Open
MickaelBergem opened this issue Jun 5, 2017 · 33 comments
Assignees
Labels
bug Bugs that need to get fixed. db/postgres high priority Priority issues
Milestone

Comments

@MickaelBergem
Copy link

MickaelBergem commented Jun 5, 2017

  1. Backed up the database
  2. Restored the database from the backup

=> Works fine ✅

  1. Back up the database
  2. Wipe out the database
  3. Do the initial django migrate
  4. Restore the same backup as in the previous case

=> I get the following error ❌

ERROR:  constraint "socialaccount_socialapp_site_site_id_2579dee5_fk_django_site_id" of relation "socialaccount_socialapp_sites" does not exist

I guess the issue comes from constraint name not being constant (2579dee5 par above).

Why not use IF NOT EXIST (https://www.postgresql.org/docs/9.0/static/sql-altertable.html) ?
Or simply wipe out the whole database before importing and remove all the DROP commands?

I guess I miss some historical data (reasons behind decisions), but for now I have a hard time figuring how to restore this dump without manual edit of the relevant lines.

@MickaelBergem
Copy link
Author

MickaelBergem commented Jun 5, 2017

I solved my issue by removing all the DROP lines, and replacing DROP SCHEMA public by

DROP SCHEMA IF EXISTS public CASCADE

but being able to load dumps in this case would be super cool :D

@ZuluPro ZuluPro self-assigned this Jun 5, 2017
@ZuluPro ZuluPro added bug Bugs that need to get fixed. database db/postgres labels Jun 5, 2017
@ZuluPro
Copy link
Contributor

ZuluPro commented Jun 5, 2017

Hello @MickaelBergem
Thanks for this issue and the proposition.

We encountered this problem several time with PostgreSQL and the DROP statements.
As solution, We plan to make Postgres binary (pg_dump/pg_restore) the default backup tool.

There's a PR about that here: #241
Does it answer to your problem ?

@MickaelBergem
Copy link
Author

Thanks for the quick reply @ZuluPro I'll try to test it next week :)

@mikeschaekermann
Copy link

I also encounter this problem when I create a backup from my staging database and try to restore it into my local development database. Is there a recommended work-around for this problem?

Thanks!

@MickaelBergem
Copy link
Author

@ZuluPro I just tried it with your branch:

$ pip install git+https://github.com/ZuluPro/django-dbbackup#pgrestore
$ python manage.py dbrestore -I backup.psql.gz -z
...
dbbackup.db.exceptions.CommandConnectorError: Error running:  psql ******* --host=postgres --port=**** --username=******  --set ON_ERROR_STOP=on --single-transaction                                
b'ERROR:  constraint "socialaccount_socialapp_site_site_id_2579dee5_fk_django_site_id" of relation "socialaccount_socialapp_sites" does not exist\n' 

It looks like this does not solve my problem :/
I am restoring against an existing database (ran migrate once + added a couple of fixtures) with a backup from a production server.

@ZuluPro
Copy link
Contributor

ZuluPro commented Jun 23, 2017

@MickaelBergem thanks for you tests
I see you are using the old connector, this PR comes with the new Postgres Binary one which uses pgrestore instead of psql.
Could you test with this one ?
Otherwise outside of dbbackup, what would be your workaround for this issue ?

@MickaelBergem
Copy link
Author

@ZuluPro I'm kind of confused here, how do I use the new connector? Doesn't the pip install take everything needed from your branch?

For now, the manual steps I take to restore a dump from another host:

  1. Remove all the lines that drop constraints, indexes, tables.
  2. Just keep the DROP SCHEMA line and append CASCADE at the end.

That's all folks.

@ZuluPro
Copy link
Contributor

ZuluPro commented Jun 23, 2017

@MickaelBergem,
You must configure DBBACKUP_CONNECTORS with dbbackup.db.postgres.PgDumpBinaryConnector.

Something like:

DBBACKUP_CONNECTORS = {
    'default': {
        'USER': 'backupuser',
        'PASSWORD': 'backuppassword',
        'HOST': 'backuphost',
        'CONNECTOR': 'dbbackup.db.postgres.PgDumpBinaryConnector',
    }
}

@mikeschaekermann
Copy link

I tried to use the new connector both with django-dbbackup=3.1.3 and the pgrestore fork.

However, I got an error No module named postgres even after manually installing it with pip install postgres.

Full error message:

root@a468a0f5530f:/code# ./manage.py dbbackup
/usr/local/lib/python2.7/site-packages/django/core/management/base.py:265: RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead
  RemovedInDjango110Warning)

ImportError: No module named postgres
  File "/usr/local/lib/python2.7/site-packages/dbbackup/utils.py", line 111, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/dbbackup/management/commands/dbbackup.py", line 58, in handle
    self.connector = get_connector(database_key)
  File "/usr/local/lib/python2.7/site-packages/dbbackup/db/base.py", line 41, in get_connector
    module = import_module(connector_module_path)
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)

Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/dbbackup/utils.py", line 111, in wrapper
    func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/dbbackup/management/commands/dbbackup.py", line 58, in handle
    self.connector = get_connector(database_key)
  File "/usr/local/lib/python2.7/site-packages/dbbackup/db/base.py", line 41, in get_connector
    module = import_module(connector_module_path)
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named postgres

Any ideas @ZuluPro ?

@ZuluPro
Copy link
Contributor

ZuluPro commented Jul 6, 2017

@mikeschaekermann Did you set any conf ?
If you are in dev env, Could you try to delete *.pyc files ?

@mikeschaekermann
Copy link

I set the following in settings.py with the user, password and host of my default database:

DBBACKUP_CONNECTORS = {
    'default': {
        'USER': 'backupuser',
        'PASSWORD': 'backuppassword',
        'HOST': 'backuphost',
        'CONNECTOR': 'dbbackup.db.postgres.PgDumpBinaryConnector',
    }
}

In which directory should I delete *.pyc files? I tried to delete them in the directory with settings.py in it, but that didn't change anything about the error message.

@thimma11
Copy link

So is it already possible to restore a backup on a fresh database and how would I do it?
Do I just need to add the Connector in settings.py or is it also necessary to add parameters to the restore command?
I'm kind of confused by this thread :/
I hope you can help me :)

@MaZZly
Copy link

MaZZly commented Dec 27, 2017

The problem with ImportError: No module named postgres
Is due to a typo in the connector settings..

'CONNECTOR': 'dbbackup.db.postgres.PgDumpBinaryConnector',
should be:
'CONNECTOR': 'dbbackup.db.postgresql.PgDumpBinaryConnector',

@mikeschaekermann

@thimma11 you should just install dbbackup==3.2.0 and then set the connector settings as above, then dbbackup/dbrestore should work.

@zxwild
Copy link

zxwild commented Feb 10, 2018

All settings specified above doesn't work for my installation, so I suppose the built-in dbrestore doesn't work at all for postgres.
(Django==1.11.9, django-dbbackup==3.2.0)

But I was able to restore database by psql directly, actually it's not so hard.

su postgres
psql -d dbname -f ./backup-file.psql

Media restore works ok, so I'm satisfied :)

@MaZZly
Copy link

MaZZly commented Feb 14, 2018

@zxwild Yeah I've found that to make the initial restoration, I can't use the dbrestore command, but using the psql command works fine

@ZuluPro
Copy link
Contributor

ZuluPro commented Feb 14, 2018

Hello all,
Could you check your backup size ?
And check if it could be contained in your tmp directory?

@zxwild
Copy link

zxwild commented Feb 15, 2018

@ZuluPro it was an initial database with about 30 records
Size was about 1 Mb, a new installation of debian 9, 500Gb HDD / 2Gb RAM.

@MaZZly
Copy link

MaZZly commented Feb 15, 2018

Yep same here.. Think it was something like 1.6Mb on a fresh site where I wanted to replicate some data from prod to dev.

@bartmika
Copy link

Major +1

@rodolfomartinez
Copy link

+1

2 similar comments
@iosifnicolae2
Copy link

+1

@jonathan-s
Copy link
Contributor

+1

@jonathan-s jonathan-s added the high priority Priority issues label Sep 19, 2020
@jonathan-s jonathan-s added this to the 3.3.0 milestone Sep 20, 2020
@abdhx
Copy link

abdhx commented Jan 8, 2021

still relevant after 3 years...

@eeintech
Copy link

Still relevant but @zxwild's workaround works just fine 😄

@moshfrid
Copy link

+1

@TheAbhijeet
Copy link

Still relevant for Django 3.2 and Python 3.10

Unfortunetly 'dbbackup.db.postgresql.PgDumpBinaryConnector' did not make any difference.

@TheAbhijeet
Copy link

TheAbhijeet commented Feb 15, 2022

As stated before the media restore works flawlessly but in case you are using dockerized Postgres and wanna migrate/copy/clone database following commands can help.

This will create a SQL file with data only

docker exec -t postgres_container pg_dumpall --data-only -U postgres > prod_dump_clean.sql

Before restoring you must run the migrations on the new host so that the new database has the tables in place before restoring.

cat prod_dump_clean.sql | docker exec -i postgres_container psql -U postgres 

@skyl
Copy link

skyl commented Nov 9, 2022

I reckon I may have just hit this with Django 3.2 and python 3.10 and django-dbbackup==4.0.2. But, I haven't tried any DBBACKUP_CONNECTORS. It seems in my case I will work around by running migrate on an empty DB to create the schema and then restore works fine.

@adamKenneweg
Copy link

have the same thing, have to run it twice

@hkhanna
Copy link

hkhanna commented Aug 14, 2023

+1

@sergeydubak
Copy link

Solved this by adding to settings.py:

DBBACKUP_CONNECTORS = {
    'default': {
        'RESTORE_SUFFIX': '--if-exists',
    },
}

@Archmonger
Copy link
Contributor

Archmonger commented Jan 16, 2024

@sergeydubak Are you interested in either documenting this, or PRing this behavior as the default for the postgres connector?

@Letme
Copy link

Letme commented Jan 22, 2024

@sergeydubak you saved my ass after 1 week of trying to figure out how to solve this (to run migrations, to use the postgres command, etc). This is great tip, and completely easy to add to the settings. I hope you can open a PR for at least the documentation upgrade, although I have a feeling this could have just been the default option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs that need to get fixed. db/postgres high priority Priority issues
Projects
None yet
Development

No branches or pull requests