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.
Related
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?
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'],
}
}
I've already a functioning website that use sqlite3 but I prefer to restart my website project with an empty project that immediately use PostgreSQL/PostGIS. At the end of the configuration of settings.py I will make a dump for upload all the sqlite3 tables into the new DB. Nobody models of the old project will modify into this new project. The differece between the old and the new project is only the DBMS.
This is settings.py:
INSTALLED_APPS = [
...
'django.contrib.gis',
]
...
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_project_name',
'USER': 'db_user',
'PASSWORD': 'password_of_db_user',
}
}
After this I've tried to test the server with python3 manage.py runserver but I see this error message:
django.db.utils.OperationalError: FATAL: Peer authentication failed
for user "db_user"
I'm sure that NAME, USER and PASSWORD are correct, the db is in localhost. I've created, a couple of week ago, another simple project to learn GeoDjango with this same settings and that website run fine.
Looks like you're missing the host field in your config:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_project_name',
'USER': 'db_user',
'PASSWORD': 'password_of_db_user',
'HOST': 'localhost' # missing part
}
}
I am building a companion web/mobile application for an existing web app. The database is implemented in MySQL. I will be writing an API using Django/Django Rest Framework (DRF).
The application is used by various organizations. The interesting part about the implementation is that each organization has it's own database. In the existing web application, the user enters the database name along with the login credentials.
How to go about implementing this in Django? I am going to have lots of models - and they all need to read from the correct database depending on the current user.
in settings.py
DATABASES = {
'default': {},
'database_one': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db1_user',
'PASSWORD': 'db1_pwd'
},
'database_two': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db2',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db2_user',
'PASSWORD': 'db2_pwd'
}
}
For sync your database, you should do like this:
python manage.py migrate
python manage.py migrate --database=database_one
python manage.py migrate --database=database_two
The first command operates on the default database, and with the --database option you can operate the database you want.
More info. can be found here: https://docs.djangoproject.com/en/2.0/topics/db/multi-db/
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