Django: Peer authentication failed for user - django

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
}
}

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?

ElephantSQL and Django not working together, atuhentication failed

I have bumped into the following problem and still don't know how to fix it. For the record I am using mac.
I would like to connect my djnago app to an elephantsql database, so I have changed the database info.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER':'abc',
'PASSWORD':'password',
'HOSTS':'tai.db.elephantsql.com',
'PORT': 5432
}
}
I see my database working fine in pgAdmin 4, so there is no issue with that, but when I run
python manage.py migrate
got the following error:
django.db.utils.OperationalError: FATAL: password authentication failed for user "abc"
Do you have any tips how to go forward?
You specify the host of the database with the HOST key, not the HOSTS for the DATAbASES setting [Django-doc]:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'password',
# ↓ HOST instead of HOSTS
'HOST': 'tai.db.elephantsql.com',
'PORT': 5432
}
}

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.

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 syncdb mysql error on localhost -> (1045, "Access denied for user 'ODBC'#'localhost')

Hoping someone can help me connect django to mysql db on localhost...
I have installed Django site into virtualenv folder on my desktop and following django docs Tutorial step by step.
I have mysqlclient installed via pip as per django docs.
I have WAMP V2.5 with latest phpmyadmin installed and operational.
The Django test site loads with localhost:8000 and I can easily setup a site with sqli.
Within localhost/phpmyadmin I created a blank database named 'test_db' and as an example for this question I used username = 'user' and password = 'pwd'.
My settings.py file is setup as below for the database settings:
DATABASES = {
'default': 'django.db.backends.mysql',
'NAME': 'test_db',
'HOST': 'localhost',
'PORT': '3306',
'USERNAME': 'user',
'PASSWORD': 'pwd',
}
}
Once I save the settings.py script, I run the command:
python manage.py syncdb
The result I get is:
django.db.utils.OperationalError:
(1045, "Access denied for user 'ODBC'#'localhost' (using password: YES)")
I can change the username and password to the root user and password and I still get the same result.
I have spent days on this Googling and Youtubing but nothing has helped so far... Any help appreciated.
Change your Database settings:
'ENGINE':'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',