I am using django-dbbackup to create a backup of my postgres db. When I run python manage.py dbbackup it creates a default.backup file.
But, when I run python manage.py dbrestore I get the following error.
Restoring backup for database: hera
Finding latest backup
Restoring: /home/dev/Documents/Program Codes/Python/Django/Hera/default.backup
Restore tempfile created: 670.5 KB
Are you sure you want to continue? [Y/n]y
Running: dropdb --username=****** --host=localhost hera
dropdb: database removal failed: ERROR: must be owner of database hera
CommandError: Error running: [u'dropdb', u'--username=dev', u'--host=localhost', u'hera']
This is about making sure the user you're using is the owner of the database. Right now it looks like you're running the dbrestore command using the dev user (from the line CommandError: Error running: [u'dropdb', u'--username=dev', u'--host=localhost', u'hera']).
There are likely three options:
1/ Change the owner of the database to dev using a PSQL command something like:
ALTER DATABASE hera OWNER TO dev;
2/ Change the --username=dev your dbrestore script uses when it runs to the owner of the database (something other than dev, you can use \l in PSQL to list your databases and see which user owns the database.
3/ You could give dev superuser and createdb ability, though not sure this is ideal in terms of security.
Related
I have a Django application which relies upon the postgis Postgres extension.
I want to copy my local Database to Heroku using pg:push, but I get numerous django.db.utils.ProgrammingError: relation does not exist errors, following on from:
pg_restore: error: could not execute query: ERROR: type "public.geography" does not exist
LINE 6: coords public.geography(Point,4326),
^
Command was: CREATE TABLE public.outreach_localcompanylocation (
id bigint NOT NULL,
long_name character varying(200) NOT NULL,
city character varying(200) NOT NULL,
country character varying(3) NOT NULL,
coords public.geography(Point,4326),
google_location_id bigint,
state character varying(200)
);
This seems to be because pg:push does not enable the postgis extension (typically enabled by running the SQL command CREATE EXTENSION postgis;).
If I try to enable it manually on a new Heroku database, then run pg:push, I get this error:
Remote database is not empty. Please create a new database or use heroku pg:reset
So is there a way to run CREATE EXTENSION postgis; as part of the pg:push process?
The docs indicate you cannot push to a non-empty database:
...To prevent accidental data overwrites and loss, the remote database must be empty. You’ll be prompted to pg:reset a remote database that isn’t empty...
https://devcenter.heroku.com/articles/managing-heroku-postgres-using-cli#pg-push
Also, are you doing your migrations? The fact that django is spitting relation errors makes me think it is leaning that way.
Delete the files in your local app's migrations folder, leave the __init__.py file there.
Run python manage.py makemigrations
Run python manage.py migrate
Reset the heroku database so it's empty and ready for you to push.
Deploy app with updated migrations to heroku.
Run heroku run python manage.py migrate
I am running a Django container on docker which depends on Postgres. Now the issue is, when I try to load the postgresql
docker-compose up
cat /tmp/dump.sql | sudo docker exec -i <container_id> psql -U <user> -d <database_name>
This imports the first few tables and gives me a lot of errors on the terminal
An example
constraint "table_id" for relation "table_name" already exists
Another example
null value in column "column_name" violates not-null constraint
Another one
insert or update on table "table_1" violates foreign key constraint "table_id"
I wanted to know is this even the right way to import this data. Plus I cant seem to drop the database as it is already being used by django.
Note:
I tried with volumes where I imported the db with postgresql. But everytime I run django, the database gets reset. I am guessing it has something to do with the migrations, but I can't touch that part.
The issue was being caused because the sql dump was trying to overwrite existing db. A quick solution if you are on dev would be to
docker volume rm <volume_name>.
And then run the cat ... command
We three working with Django and postgres is the database. Whenever we push the code to GitHub. The database data is not reflecting. The data I stored is visible to me only. The postgres user, password and database name are same on all our laptops. How to make when I push that has to go their databases also.
If you are talking about changes in db schema, then take a look django migrations - https://docs.djangoproject.com/en/3.1/topics/migrations/. The workflow is following:
change model (e.g. add new field, change existing field...)
generate migration file by running python manage.py makemigrations. This generates migration file in <app_folder>/migrations
Run python manage.py migrate to apply changes in models to your database
Add migration file to your version control (github or whaever) and push it
Now when your colleagues gets updated code from version control they need to run python manage.py migrate to apply model changes to their local database.
I found that it is not possible to send postgress data directly. But sqlite can be used for it. This link aids in that - https://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/ . But i send data by taking backup like
From Postgres to Postgres: taking backup :
pg_dump dbname > dbname.bak
and then on new Postgres restored with:
psql test < dbname.bak
I am migrating a project from Django 1.1 to Django 3.0 I am done with the project. When I am dumping the production dump to my local in the newly converted project I get "Table already exists".
Here's what I am doing.
mysql> create database xyx;
docker exec -i <container-hash> mysql -u<user> -p<password> xyx < dbdump.sql
then I run the migrate, as I have had to do some changes to the previously given models.
./manage.py migrate --fake-initial
this is the output I get
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1050, "Table 'city' already exists")
So, what to do ?
Alright boys and girls, here's the approach I followed to solve this problem.
I dumped the entire database.
docker exec -i <container-hash> mysql -u<username> -p<password> <dbname> < dump.sql
Now I listed all the migrations I made using
./manage.py showmigrations <app-name>
This will give me the list of all the migrations I have applied, now from inspecting the migrations, I realized that from the 7th migration to the 30th migration I had done my changes.
Here's the tedious part which any sys admin can write a script to do in less than 4 lines of bash script. You can generate the raw SQL of any migration with this command.
./manage.py sqlmigrate <app-name> <migration-name> > changes-i-made.sql
Now that I have created my changes-i-made.sql file I'll need to run this script 22 more times but with >> otherwise everytime you run the command with a single > it will keep overwriting your changes file.
Now once all of your migration changes are recorded inside a file, open up your sql shell connect to the database and start pasting the changes or do some sql magic to pick all the changes directly from the file.
Once you're done go ahead and fake all the migrations, cause you don't need Django to do them you already did.
./manage.py migrate --fake
and then login to your production instance and get ready to fuck with your senior team lead who said you couldn't do it.
I just checked to see if this approach is working and the future migrations will be working, so I created one and everything works like a breeze.
I installed OSQA using the bitnami installer and everything worked fine. Now, I am hacking at the osqa code base. If I need to restore the database to its initial state, do i need to reinstall OSQA or is there any command to truncate the database and load intial data.
I tried using use_osqa.bat and did a python.py manage migrate forum but it didnt work.
It uses a postgresql database
You can use the django-admin.py flush:
Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any post-synchronization handlers will be re-executed, and the initial_data fixture will be re-installed.
django-admin.py flush
Finally, this worked for me. Note that this is applicable only for OSQA using the bitnami environment with postgresql database.
if you want to restore your database state to the original version, first connect to postgres database using admin credentials(use any client, I used Dbeaver)
database : postgres
username : postgres
password : **admin password** : this is the same password you gave while installing bitnami-osqa
Now, drop the bitnami-osqa database and create it again(if you already have connections to bitnami_osqa, close them)
Drop-database bitnami_osqa;
Commit;
Create-database bitnami-osqa;
Commit;
Now open use_osqa.bat in your bitnami root folder and execute the following
cd apps\osqa
python manage.py syncdb --all --verbosity 2
[specify yes when it prompts to create super user and create some user]
python manage.py migrate forum --fake