django - error adding database to heroku - python

I am struggling to deploy my app on heroku. Mainly due to database configuration.
This is the bottom of the settings.py file,
import dj_database_url
DATABASES = { 'default': dj_database_url.config() }
try:
from .local_settings import *
except ImportError:
pass
This is my local_settings.py file,
import os
BASE_DIR = os.path.dirname(os.path.dirname(file))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}
I can run heroku local web successfuly.
But when I run , heroku run python manage.py migrate, it gives me error,
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
When I run, heroku run python manage.py shell,
And then run
>>> import dj_database_url
>>> dj_database_url.config()
I get the dictionary giving me the full information about the database.
Where is the issue ?

Your local_settings file should not be deployed. It is for local use only, hence the name. You should exclude it from your git repo.

Try importing the settings from local_settings.py first, then change the DATABASES setting,
import dj_database_url
try:
from .local_settings import *
except ImportError:
pass
DATABASES = { 'default': dj_database_url.config() }
You have defined a DATABASES variable in the file, but when you are importing the local_settings.py, your DATABASES configuration is being overridden by the development values. You should do the import first and then change the DATABASES option.
Personally, I'd suggest that you should keep a base_settings.py to contain every settings other than databases, then import the same into the heroku_settings.py, rather than importing the whole local_settings.
Keeping a base_settings, development_settings, heroku_settings(or production_settings) would make it easier to handle the deployment.

Related

Unable to makemigrations with PostgreSQL setup

I am trying to setup PostgreSQL locally on my computer, but when I try to initially set up my Django application with it using python manage.py makemigrations, I am given this warning:
RuntimeWarning: Got an error checking a consistent migration
history performed for database connection 'default': fe_sendauth: no password supplied
My database table in my settings.py is as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get("DB_NAME"),
'USER': os.environ.get("DB_USER"),
'PASSWORD': os.environ.get("DB_PASSWORD"),
'HOST': os.environ.get("DB_HOST"),
'PORT': os.environ.get("DB_PORT"),
}
}
My .env file is located in the same directory as my manage.py file:
DB_NAME=Smash
DB_PASSWORD=root
DB_USER=SmashDatabase
DB_HOST=localhost
DB_PORT=5432
DEBUG=1
I tried following this link's instructions but none of the offered solutions fixed the problem. I don't know what is wrong or what causes this issue.

How can I push Django code using dj-database-url, psycopg2 to Heroku?

I am currently building a web project in Django and working on getting the site ready for deployment. I initially deployed the site on Heroku using Sqlite3, with my database code in settings as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
However, due to Heroku's ephemeral file system I realized I needed to switch to Postgres. After following a few different guides I arrived at the following changes to my settings. I first deleted the "DATABASES" as mentioned above and replaced it with the following:
import dj_database_url, psycopg2
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': *************,
'USER': **************,
'PASSWORD': ************************************,
'HOST': *********************,
'PORT': '5432',
}
}
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
When I make these changes everything works perfectly on the local Django development server (127.0.0.1:8000), but once I push the changes through Git and to Heroku I try opening my site on Heroku and get "Application Error" and a suggestion to check my logs. Which report "ModuleNotFoundError: No module named dj_database_url" and similarly any outside library I try to upload to Heroku appears to have this issue. How can I fix this issue and move my site into production? Any help would be greatly appreciated.
You need to include them in your requirements.txt file or however the buildpack you're using expects the applications dependencies to be indicated.

How can I connect to MySQL on Heroku?

I have a Django project that uses MySQL as its database. On my development machine the MySQL database runs on my local machine as shown here:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'USER': 'root',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
#'PORT': '3306',
}
}
I have successfully deployed the project to Heroku, but it keeps showing that
2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)
since the setting is the local MySQL and the views.py contain functions that import local database. I still cannot figure out how to have the website to connect to this database.
Is there a way to make this work with Heroku, or do I need to find another host?
juanmhidalgo's answer is a good start, and can be generalized for arbitrary environment variables. But if you only care about the database variable there's another solution.
By default, Heroku provides a PostgreSQL database and sets your application's DATABASE_URL with a connection string that can be used to connect to it.
As far as I know, the various MySQL addons also set this variable but it would be helpful to know which one you've selected so we can confirm. It may need to be set manually, based on another environment variable.
Assuming that the DATABASE_URL environment variable is properly set you can use dj-database-url to set your database up directly, optionally providing a fallback to use when the variable isn't available (e.g. on your development box), in your settings.py file:
import dj_database_url
DATABASES['default'] = dj_database_url.config(
default='mysql://root:<password>#localhost:3306/<database>',
)
You can promote the settings.py to a package. Create the settings folder like this one
settings/
├── __init__.py
├── base.py
├── dev.py
├── heroku.py
Keep the base.py as your settings.py and then, based on the env, changes what you need.
Your heroku.py could be
from .base import *
DATABASE = {
# change to use the database on heroku
}
What is important is to tell the deploy version to use the new settings. Check the wsgi.py
import os
from django.core.wsgi import get_wsgi_application
# point to the new settings.dev as default, if the `DJANGO_SETTINGS_MODULE`
# is not set
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev")
application = get_wsgi_application()
Remember to change the DJANGO_SETTINGS_MODULE in heroku to settings.heroku
A really useful way to do this is to use django-environ and set the configuration option on the heroku settings.
# settings.py
# Parse database connection url strings like psql://user:pass#127.0.0.1:8458/db
DATABASES = {
# read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
'default': env.db(),
}
and your environment variable like:
DATABASE_URL=mysql://user:%23password#127.0.0.1:3306/dbname
Regarding your question, Heroku is a really good option to deploy your projects.

Django psycopg2.OperationalError: fe_sendauth error but not connecting to postgresql database

I am using the following sqlite connection in my myapp/settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':testdb:',
#I also tried 'NAME': 'testdb',
},
}
in my manage.py file I am using:
if __name__ == "__main__":
os.environ.setdefault("myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
On running ./manage.py migrate on the command line I get the following error:
django.db.utils.OperationalError: fe_sendauth: no password supplied
I tried removing psycopg2 and re-ran the migration and get the following error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
I can't work out why django is trying to connect to a psql database where the only DATABSES configuration in the application is for sqlite3.
Could this be due to a dependency?
Django only uses DATABASES setting to generate migrations using a specific database driver. I believe somewhere, there is an import which is overriding your DATABASES setting. These are possible places to look for debugging in order of priority.
Any import into settings.py file after you set DATABASES variable which might override it.
if above is not the case, Some dependency must be overriding this setting manually. you might want to look into your INSTALLED_APPS.
If still not enough, you might need to look at any other dependency that you are using in your project which tries to interact with django.
Try this,
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
This might be helpful for you.

How to connect MySQL Database in django 1.10.6

import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.

Categories