Unable to makemigrations with PostgreSQL setup - python

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.

Related

Cannot connect to postgresql databse from django settings.py on localhost

Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.

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.

Creating SuperUser on a custom database (not default)

I am currently working on a multi-tenant django application. Since the requirement of multi-tenancy, for starters, I have created a second database. However, I am unable to create a SuperUser in the newly created database.
I did try the answer listed Django fails to create superuser in other db than 'default'
However, no luck.
Getting the following output on compile:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly co
nfigured. Please supply the ENGINE value. Check settings documentation for more
details.
Settings.py
DATABASES = {
'default': {},
'Alora': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Alora',
'USER': 'postgres',
'PASSWORD': 'somepassword',
}
}
Python Version
C:\Users\User>python --version
Python 3.6.3
Django Version
C:\Users\User>django-admin --version
2.2
Tried python manage.py createsuperuser --database=Alora.
Received the error stated above.
Any help is deeply appreciated.
Thanks!

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.

Setup and connect to database for Django on AWS

I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate

Categories