Django 1.8 migrating local postgres database into a heroku database - django

I've been stuck on this part for a while.After completing all the other steps when I enter git push heroku master, it pushes all the files but how do I migrate my local database? There are no commands for that. My settings.py file is still pointing to the local postgres database.How do I migrate this data into the heroku database?
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'music',
'USER': '**',
'PASSWORD': '**',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

Did you followed the heroku django tutorial. From the django-settings they are clearly mention how heroku will detect database url using dj-database-url package.
So just define this setting below your settings.py file then you are good to go.
For Importing and Exporting to heroku postgres please see this heroku-postgres-import-export

Related

Heroku Posgres' DATABASE_URL cannot be loaded in Django's setting.py and application error occurs

When building an API server using Django+REST framework and uploading it to Heroku, hard-coding Heroku Postgres' DATABASE_URL in the DATABASES of setting.py as follows will work on Heroku without problems. (xxx is originally the information of DATABASE_URL, but we dare to hide it this time)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'xxx',
'HOST': 'xxx',
'PORT': '5432',
'USER': 'xxx',
'PASSWORD': 'xxx,
}
}
However, you can add a .env file to your Django project, stating the following (where xxx is the information originally entered for the DATABASE_URL, but we've dared to hide it this time)
DATABASE_URL=postgres://xxxx
In setting.py
import dj_database_url
from dotenv import (
find_dotenv,
load_dotenv,
)
load_dotenv(find_dotenv())
DATABASES = {
'default': dj_database_url.config(conn_max_age=600),
}
If I put something like this, it works in the local environment, but when I upload it to heroku, I get an application error.
Do you know the cause?

Django not using correct Postgres schema on Heroku

I have a Django app that I need to connect to a schema "new_schema" by default, rather than public. I've managed to get this working on my local instance by running
CREATE SCHEMA new_schema;
on the local database & then adding into my settings a database config like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS': {
'options': '-c search_path=new_schema',
},
'NAME': 'database_name',
'USER': 'user_name',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '5432',
},
}
But, when I deploy to Heroku & create the schema there, I can't get the app to point to the new schema. In my production settings file, I have
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
DATABASES['default']['OPTIONS']['options'] = '-c search_path=new_schema'
in order to get the Heroku database url into the right config format, and then add in my schema to the options. But for some reason this doesn't work - instead any migrations get applied to public as do db queries.
I can explicitly set the new_schema in the Postgres users search_path, but there are other servers that need different default schemas
I have been going in circles trying to resolve this for ages, I don't know what I'm missing! I can' tell if this is a problem with my Heroku/Postgres setup, or my Django app.

Sync all tables from sqlite to postgres

I have a django application and deployed it on DigitalOcean. But the only problem is, new models, admin models, tables are not showing in django admin dashboard which is on running on server. Although I pushed al changes to github, pulled them from, and made migrations, again nothing changes. How can migrate all tables from db.sqlite3 to postgresql ?
I would bet your settings.py database is still configured to the default sqlite.
# default settings.py using SQLlite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Change that to your actual postgresql database address
Since you're deploying, it is best practice to configure those parameters as separate environnment variables, as such in your settings.py. Environnment variables would then have the actual values.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['POSTGRE_NAME'],
'USER': os.environ['POSTGRE_USER'],
'PASSWORD': os.environ['POSTGRE_PASSWORD'],
'HOST': os.environ['POSTGRE_HOST'],
'PORT': os.environ['POSTGRE_PORT'],
}
}

Changing from Sqllite to Postgres on Heroku- why it is not working?

I am a bit confused , hope someone can help me out
Originally I uploaded my django app to heroku account with sqllite db
This is what I had in my settings file for DB
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
} }
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
It didnt work great since SQLlite was getting flashed to its original state every 24 hours (but worked perfectly since I needed demo system)
However now I have to make it productive so I want to change the db to connect to postgres . So I used credentials from postgres DB I created with heroku and my db in settings looks like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'd4drq1yytest',
'USER': 'xvvqgkjtest',
'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45',
'HOST': '777-77-77-67-7.compute-1.amazonaws.com',
'PORT': '5432',
}
}
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
But even I changed the settings file and redeployed the new file I still see that my heroku app is connected to my sqllite DB.
What did I do wrong? (I deploy using a master brunch from my github.)
If you have not already, you might need to add heroku postgres as an app in your heroku app's dashboard.
It seems like you have used the same code that I used to connect my heroku's postgres to heroku. You should only have to state
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
then make sure you have pip installed whitenoise and added that to requirements.txt. After commmiting, and pushing to heroku, you may want to run heroku run bash on cmd or terminal. Then you may want to run python manage.py makemigrations yourappname afterwards, you may want to run python manage.py migrate, and if you get no errors, you might want to check on your heroku dashboard for your app, clicking on postgres, to see if any of the postgres' tables are in use. If they are, then you are using the heroku postgres.
you may not need the lines of code below for heroku
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'd4drq1yytest',
'USER': 'xvvqgkjtest',
'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45',
'HOST': '777-77-77-67-7.compute-1.amazonaws.com',
'PORT': '5432',
}
}
this should be where you are calling the database you will be testing with your localhost.

Django on Heroku - settings.py for development vs. production

I deployed a django application to Heroku using this tut.
And I know I can use heroku local to run the app locally.
But how do I migrate locally? as in create a database locally, not in the cloud, rather than push changes to the cloud and see if it works?
You should be able to override the DATABASES setting for your local environment to point to a different database:
# settings.py
. . .
# at the end
try:
from local_settings import *
except ImportError:
pass
# local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'whatever',
'USER': 'whatever',
'PASSWORD': 'your-password',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}