I am having trouble migrating my development database onto Heroku. This is for a personal project written in Django.
settings.py
db_from_env = dj_database_url.config()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DATABASES['default'].update(db_from_env)
I've migrated the database locally and pushed the migration onto Heroku already. Then, I ran the heroku run python manage.py migrate command to migrate the database on Heroku.
I had accidentally ran makemigrations on Heroku, but have restarted my dynos since.
Currently, my database on Heroku is not populated at all.
Related
I'm trying to add an existing sqlite3 database to my Django project but it doesn't seem to be recognized.
I've added that database to my project folder, modified the settings.py file to reflect that addition:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'added_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'added_db.sqlite3'),
}
}
I then ran the 'python manage.py makemigrations' and the 'python manage.py migrate' commands but it doesn't seem to do anything. When I run 'python manage.py inspectdb', it only shows the tables within the default database.
What am I missing?
Depends on document:
The migrate management command operates on one database at a time. By default, it operates on the default database, but by providing the --database option, you can tell it to synchronize a different database.
So, you must provide your db name when you make migrate.For detail you can see here
./manage.py migrate --database=added_db
I have created django project with postgres database I have deployed my project into heroku server, and also migrate with
heroku run python manage.py migrate
I want to push my stored database to heroku database,
PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
but getting
> error shiv#shiv:~/Documents/projects/django_related_project/zoedp$
> heroku pg:push applications postgresql-curly-07168 --app application
> heroku-cli: Pushing applications ---> postgresql-curly-07168 ▸
> Remote database is not empty. Please create a new database or use
> heroku pg:reset
I also run command heroku pg:reset and again try again
this time I got error
shiv#shiv:~/Documents/projects/django_related_project/zoedp$ PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
heroku-cli: Pushing edpapplication ---> postgresql-curly-07168
pg_dump: [archiver (db)] connection to database "application" failed: FATAL: Peer authentication failed for user "edpuser"
pg_restore: [custom archiver] could not read from input file: end of file
▸ pg_dump errored with 1
here is my setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'applications',
'USER': 'edpuser',
'PASSWORD': '1qazxsw2',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
and overwriting pg_hba.conf deny me.
what should I do?
I was having this issue, is more related to postgresql auth configuration rather than heroku pg:push command
I would recommend this approach:
Set a password for the postgres user:
Based on this amazing answer: https://stackoverflow.com/a/26735105/3172310
At this moment:
You have set a password for the postgres user and configure postgres to ask for id when using psql, which seems pg command does.
then run command as this:
PGUSER=postgres PASSWORD=pg_password heroku pg:push local_db_name DATABASE_URL --app application_name
Replacing:
pg_password: password for the postgres user
local_db_name: your local database
application_name: heroku app name
and should work fine!
I use PyCharm create a website project, but do not find the db.sqlite3 file.
The database settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
EDIT
You see the leftside bar there is no db.sqlite3 file, and I reveal in Finder still can not find it.
If there is no db files default, you should execute those command in PyCharm terminal:
python manage.py makemigrations
python manage.py migrate
Then you can see it.
I am trying to link a Heroku Postgres databse with my Django application. I have created the database and linked it to my Heroku application using this tutorial. I can't seem to get it working with Django, however.
I am able to access the database through the heroku pg:psql command. But when I try to run python manage.py migrate, Django gives...
settings.DATABASES is improperly configured
Please supply the ENGINE value.
In settings.py, I have...
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
I have already promoted the correct heroku postgres database to my app's DATABASE_URL variable, which I can verify through heroku config.
You've misread the instructions. It should be:
DATABASES = {
'default': dj_database_url.config()
}
dj_database_url already parses the env var, you don't need to pass it explicitly.
Heroku config variables aren't available in Django settings.py when testing locally.
https://stackoverflow.com/a/21763381/3783608
I've got an Openshift/Django issue:
I'm having difficulty getting stylesheets and my SQLite database working in Openshift. When attempting to access a page that uses the database, I get the following Django debug message: "no such table: mytable". I get the same in rhc tail. Everything else appears to work.
My local file setup:
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here
mydatabase.db
For openshift, I modify the directory as follows:
openshift appname
wsgi
project
project
settings.py etc
app
normal django files and custom code here
templates
my templates are here
static
stylesheet is here (NOT WORKING)
mydatabase.db (NOT WORKING)
Relevant bits of my setup.py file:
BASE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
STATIC_PATH = os.path.join(BASE_DIR, 'static')
DATABASE_PATH = os.path.join(BASE_DIR, 'mydatabase.db')
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATABASE_PATH,
}
...
STATIC_ROOT = ''
STATIC_URL = '/static/'
Any ideas? It works fine when running locally.
You'll want to checkout this blog post that goes over database configuration with django.
https://www.openshift.com/blogs/rapid-python-and-django-app-deployment-to-the-cloud-with-a-paas
You are likely getting the "no such table" error because the table does not actually exist (or the database itself does not exist). You do not mention running python manage.py migrate (or for Django before version 1.7 python manage.py syncdb). Without having run that command the table will not be automatically created.
In your case I think the database does not exist. Every time you do a git push to openshift, all files in your account will be overwritten except the special folder referenced in $OPENSHIFT_DATA_DIR. This is the only folder that is guaranteed to be saved and persisted across updates. You may have gone through the trouble of running migrate before and then on your next git push the database was destroyed because you had it set to be stored in your project directory. To ensure that your sqlite database is persisted across pushes you should update your settings file as follows:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.environ['OPENSHIFT_DATA_DIR'], 'db.sqlite3'),
}
}
...
On Openshift you need to use action hooks to run the migrate command. This is done by creating a folder called .openshift/action_hooks inside of your project folder. Within the folder create an executable bash script called deploy.
project
.openshift
action_hooks
deploy
project
app
templates
static
The deploy file should contain the migrate and collectstatic commands. migrate will create the tables and collectstatic will copy all of the files in your static folder to the location specified in your settings under STATIC_ROOT. When running with DEBUG=false Django will not serve static files automatically. Openshift will use apache to serve the static files that are stored in wsgi/static so you should move the static folder up one level on Openshift and set STATIC_ROOT to point there. This should solve the stylesheet problem.
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi/static')
project/.openshift/action_hooks/deploy:
#!/bin/bash
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
cd $OPENSHIFT_REPO_DIR
echo "Executing 'python manage.py migrate'"
python manage.py migrate
echo "Executing 'python manage.py collectstatic --noinput'"
python manage.py collectstatic --noinput