Migrate Django App from Heroku to Linux Server - django

I had my Django app running on Heroku, but now I want to migrate it to my own web server. Everything went fine except the PostgreSQL database: I exported the database from Heroku and imported the dump file into my own PostgreSQL. When I run python manage.py syncdb i get the following error:
django.db.utils.ProgrammingError: no schema has been selected to create in
When I open any page in my browser I get this error:
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM "django_se...
What is the best way to migrate the app (and the database)?

You probably don't have a public schema in your database. Run this in your database to create it:
CREATE SCHEMA public;
After that you don't need to run syncdb, just run:
./manage.py migrate

Related

How to add Postgres extension when pushing local database to Heroku

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

Django Migrations not updating Heroku PostgreSQL database

I've been stuck on an issue for a while where I have a model that I've created locally (class User(AbstractUser)), ran "python manage.py makemigrations" and "python manage.py migrate", which works and interacts correctly with the django server when run locally.
When I push this to heroku however, I receive an error response to certain api requests which essentially say "relation "app_user" does not exist". I assumed this was because somehow the postgreSQL db didn't receive the migrations when the app was deployed to heroku. After logging into the db I noticed that it had a few other tables, including "auth_user" (I assume this is a default django table), but not "app_user". I have done "heroku run bash" and "python manage.py migrate" in an attempt to migrate the changes to the postgreSQL db, but this doesn't have any effect.
I'm not sure if this will be useful but the postgreSQL server I'm using is the standard Heroku Postgres Hobby Dev add-on.
My questions are:
Should running "python manage.py migrate" on heroku update the postgreSQL database? If so, how does this work?
Given that the migrations are all on heroku, how can I manually update the postgreSQL with the migrations?
Fixed by deleting old heroku db and creating a new one. Unsure what exactly caused the issue above but I feel it may be because we deleted all migrations on the repo at some point in the past (to start from scratch), which meant the current db state was not applicable to migrations in the repo and so could not be applied. After doing this, running migrate on the remote server works perfectly.

Django/ Heroku Deployment (ProgrammingError at / relation "posts_post" does not exist LINE 1)

I'm Sorry to ask again, but i haven't gotten any solutions. my django blog works fine locally but after deploying to heroku, i started having an error. Please what can I do to resolve this?
The error states: "ProgrammingError at / relation "posts_post" does not exist LINE 1: ...evious_post_id", "posts_post"."next_post_id" FROM "posts_pos..."
Open command line of heroku :
heroku run bash
Make migration of database :
py manage.py makemigrations
Migrate database :
py manage.py migrate
Create super user :
py manage.py createsuperuser
Login as admin at your hosted site:
https://website url/admin
This will not upload your data at local server.
You may add data by creating superuser at heroku server
I don't have much details here, but relation does not exist means the table is not created.
So I suspect when deploying to heroku you created a new database but you haven't run the migrations for it.
So try to run the python manage.py migrate command on the heroku database.

How can I import a local postgres file into my Heroku postgres database?

My django application is working perfectly fetching and inserting data into local postgres database. Now i have data in my local postgres database. When i deployed my app on heroku and run migrate command in heroku then it create the tables. But the tables are empty.
I just want to know that Did i have to insert all my data again in heroku postgres database or there is any way to migrate/export the local postgres database with data to heroku postgres database?
you can use django-admin dumpdata and django-admin loaddata commands
run the first command in your local machine and upload the generated file to your server and run the loaddata command on your server to restore your data
further reading example docs

Django: Integrity error installing fixtures

Foreground
Hi. I´m uploading my Django app database to my Heroku production environment. When I try to manage.py loaddata I get the following error:
django.db.utils.IntegrityError: Problem installing fixtures: insert or update in table "catalog_contactos" violates key constraint.
I found this post where someone suggests that first you should load models that are the base for foreign key: related post
Also I found this post that refers to moving a database to another app (the production version for example). Basically it says:
When you backup whole database by using dumpdata command, it will
backup all the database tables
If you use this database dump to load the fresh database(in another
django project), it can be causes IntegrityError (If you loaddata in
same database it works fine)
To fix this problem, make sure to backup the database by excluding
contenttypes and auth.permissions tables
Link to the post
My code
Following that suggestions I do:
python manage.py dumpdata --exclude auth.permission --exclude contenttypes > data.json
Then on the server I run the following code and get the integrity error:
heroku run python manage.py loaddata data.json
Should I dumpdata model by model and loaddata them in a certaing order? Or I´m coding something wrong? Any clues?
Thanks in advance!
Update
Could this "Natural keys" issue be the solutuion? Django serializing objects
When I had integrity error with django/postgreSQL I did DROP TABLE table in postgreSQL.