I am trying to migrate a DB from sqlite to postgresql...so I typed:
sudo -u postgres psql
postgres=# ALTER USER postgres WITH PASSWORD 'newpassword';
and the output returns ALTER ROLE
but when I type python manage.py migrate I receive always the same error:
django.db.utils.OperationalError: FATAL: password authentication
failed for user "douglas"
This is the database sections of my settings.py.
# Old, using mysqlite
"""
DATABASES = {
#'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#}
'default': dj_database_url.config(default='postgres://localhost:5432/postgres_db_name'),
}
"""
# New, using postgres
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'douglas_db',
'USER': 'douglas',
'PASSWORD': 'vamointer',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Note: When I run the 'ALTER USER postgres WITH PASSWORD' I put in the same password defined in the settings.py.
The SQL you are running does not match the user you are attempting to use.
You will need to create the user if it does not exist:
CREATE USER douglas WITH PASSWORD 'vamointer';
or if it does exist, change that user's password instead.
ALTER USER douglas WITH PASSWORD 'vamointer';
Once you have done that you should have more luck. You may need to assign permissions to that user as well.
If you are bone-headed like me and have used 'USERNAME' instead of 'USER' in your Django database configs in settings.py, make sure you change it to 'USER' else you will see this same error. Hope this helps someone like me down the road.
Special characters in postgresql are converted to different characters while execution. Make sure you do not have special characters (#,$,etc..) in your password.
If you do, change the postgresql password as follows:
sudo -u postgresql psql
postgresql=#ALTER USER yourusername WITH PASSWORD
'set_new_password_without_special_character';
Make sure you do not forget the ; at the end of postgresql command.
Then run python manage.py and it should work!
You can try this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'telusko',
'USER': 'postgres', #in place we should always be write USER only if we write USENAME then it will give error.
'PASSWORD': '1234',
'HOST':'localhost',
}}
In my case, I had to change
'ENGINE': 'django.db.backends.postgresql_psycopg2',
to
'ENGINE': 'django.db.backends.postgresql',
Hope it helps someone!
It's also possible that your PostgreSQL server is not running. Please run next command, to check if postgres is running:
sudo service postgresql status
If not please run it, using:
sudo service postgresql start
Also, you can have wrong port in your settings. To check where Postgres is running use:
sudo netstat -plunt |grep postgres
And after update PORT in DATABASE config in Django settings
Another reason why this happens is if you have a conflicting version of postgres running. I had one running in docker and another on my system.
systemctl stop postgresql to stop the system version and purge it if you'd rather use only the docker version.
Try this for creating the user for Postgres
postgres=# create user username with encrypted password 'password';
Add permissions
postgres=# grant all on database db_name to username;
other useful commands might help
sudo -u postgres psql
\du+ #list of user
\l+ # list of DB
Credits to this article
Note: the database name and the username are always in lowercase, even if you create them with capitals.
For me it was as simple as using capital letters in my db and user name.
It seems that postgres automatically ignores case:
postgres=# CREATE USER MyProjectUser WITH PASSWORD 'password';
is then stored as:
postgres=# \du+
List of roles
Role name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
myprojectuser | | {} |
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
So in my database, this:
DATABASES = {
'default': {
.
.
'USER': 'MyProjectUser',
'PASSWORD': 'password',}}
wasn't recognized & threw password auth failure error.
Wanna do my part as a fellow developer here who got this issue too
You might be following a sometimes dubious "by the book guide" to set up PGSQL with Django. Some of the guides do not require us to write down the HOST and PORT in the DATABASES section. Like this :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'urdb',
'USER': 'your_username',
'PASSWORD': '1234',
'HOST':'localhost',
'PORT':''
}}
I fixed this by :
Check my actual host and port, in my case, the default port is 5433
Put 5433 in the PORT
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'urdb',
'USER': 'your_username',
'PASSWORD': '1234',
'HOST':'localhost',
'PORT':'5433'
}}
Et voila! No more authentication issues!
Don't expect your environment nor codes to know everything! :)
I had not exactly the same problem cause mine was with docker-compose, django but mainly with postgres in which in steps of building django project (note deletion of database wasnt an issue) I couldnt migrate my database so by deleting data\db folder I attempted to solve it. for more information on my problem Django Postgres docker-compose connection error mainly failed: FATAL: password authentication failed for user foobar ERROR: 2
Related
I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
I've checked user's permissions and I'm sure that this user have permission to create database.
My database config looks like this:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '****',
'PORT': '****',
}
}
My postgres db is on a server.
My questions are:
What is the right way to config my db and run tests?
Should I be using sqlite for testing?
If so how my code should look like, so I don't have to comment configs?
It looks like your DB user doesn't have permission to create a new database. Please, take a look here. This command-line utility allows you to create a user and set their permissions.
Example:
createuser my_user --createdb -W --username postgres
Note: you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.
Answering your questions:
You may have several configs for different stages, e.g development, testing, production.
You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.
I am an extreme newbie to Django and using shell. Hence please be gentle. I am working on a site where the owner has lost the relationship with the developer and hence passwords to the admin accounts (front-end and back-end). I am trying to create superusers for both but am having problems with the database. The site uses a PostgreSQL database. In the shell I activate the virtual environment and run my command:
python3 manage.py createsuperuser
The email address is requested and enter but receive the error after several lines of script.
django.db.utils.OperationalError: FATAL: password authentication failed for the user "xxx".
Do I need to somehow activate or enable the connection to the database before running the command?? Again really new and not trying to be the developer on the site- just trying to gain access and create users. Many thanks.
ADDITION BASED ON CONVERSATION BELOW
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'hci',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
except from base.py
from .base import *
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = True
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
DATABASES['default'].update({
'NAME': 'xxx_hcidemo',
'USER': 'xxx_hcidemo',
'PASSWORD': 'xxxxxxxxxx',
'HOST': 'localhost',
})
BROKER_URL = 'redis://localhost:11201/0'
CELERY_RESULT_BACKEND = 'redis://localhost:11201/0'
entire demo.py file with password and username removed....
Solution was found! Many thx- the manage.py file was pointing at demo database - had to change to point at the production database. Completely agree - bad set-up but I now have access.
Probably You (or someone else) change password to database.
Look at Your DATABASES settings in Your settings.py file and update PASSWORD field.
EDIT:
Your manage.py use different database settings, than site. Remove if __name__ == '__main__': from manage.py file and try to add user.
BTW. It's very bad solution to differentiation prod env from dev env in this way.
I'm new to Django and MySQL, and am trying to continue a project written by a previous developer.
I've just finished a fresh install of MySQL (didn't set any password). When I run python manage.py syncdb, I get the error:
python manage.py syncdb
/home/home/.virtualenvs/sorrento/local/lib/python2.7/site-packages/debug_toolbar/settings.py:134: DeprecationWarning: INTERCEPT_REDIRECTS is deprecated. Please use the DISABLE_PANELS config in theDEBUG_TOOLBAR_CONFIG setting.
"DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)
OperationalError: (1045, "Access denied for user 'home'#'localhost' (using password: NO)")
This is the database config in settings.py:
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '',
}
}
########## END DATABASE CONFIGURATION
Since NAME is empty, does this mean there isn't a database set up? Do I need to get a database dump from the previous developer? Or is there something else I'm missing?
It is telling that you don't have permissions to edit the DB. To be able to connect you will need the NAME of the database and a USER and PASSWORD to connect with.
You don't need to get a copy of the db from the previous developer unless you actually need any of the data from there.
You have a fresh install of MySQL, so you just need to create an (empty) database.
Firstly, edit your db settings so that the USER attribute is 'root' and the NAME attribute is a relevant name (perhaps the name of your project).
Then, from the shell, do mysql -u root and then CREATE DATABASE <dbname> where dbname is the NAME you used above.
Now you should be able to run syncdb.
I'm trying to move over from SQLite3 to MySQL, after much difficulty I finally got MySQL-python working however when I try to run ./manage.py syncdb I get an error
OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
Right now I'm running MySQL through MAMP. I tried creating a new user instead of using root but I get the same error. Any help/advice would be greatly appreciated, thanks.
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db17', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '8889', # Set to empty string for default.
}
}
Those are the settings given by MAMP.
In addition to creating a new user, did you also grant that user permissions on the tables in your project?
GRANT ALL PRIVILEGES ON db17.* TO 'root'#'localhost';
I am attempting to get my first Django project working, on Windows, but I get an error message that reads:
File "c:\users\[username]\appdata\local\temp\easy_install-pazcre\MySQL_python-1.2.3-py2.7- win32.egg.tmp\MySQLdb\connections.py", line 187, in __init__ mysql_exceptions.OperationalError: (1045, "Access denied for user 'django_user'#'localhost' (using password: YES)")
I created my database from the command line as:
-- create the database
CREATE DATABASE GlobalXdb CHARACTER SET utf8;
-- create user
CREATE USER 'django_user'#'localhost' IDENTIFIED BY 'thepassword';
-- give user permissions to db
GRANT ALL ON django.* TO 'django_user'#'localhost'
My settings.py file contains:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'GlobalXdb', # Or path to database file if using sqlite3.
'USER': 'django_user', # Not used with sqlite3.
'PASSWORD': 'thepassword', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
Can anyone shed some light on what I have done incorrectly?
Your database is named GlobalXdb and yet in this line...
#give user permissions to db
GRANT ALL ON django.* TO 'django_user'#'localhost'
you grant permissions to django_user on database named django.
Give permissions to the correct database GlobalXdb should solve your problem.
The error message says Access denied for user 'django_user'#'localhost' so my guess would be that the user 'django_user' doesn't exist or you typed the password wrong.
Another less likely suggestion would be to check the rights granted for 'django_user'. It's possible that this user doesn't have permission to create tables.
This is solved by defining two DATABASE profiles in your settings.py.
default - Used by django at runtime with limited permissions for that extra security.
sync - Used by syncdb, as user root, with extra create privileges.
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME':'database_name',
'USER':'runtime',
'PASSWORD':'runtime_password',
'HOST':'',
'PORT':'',
},
'sync': {
'ENGINE':'django.db.backends.mysql',
'NAME':'database_name',
'USER':'root',
'PASSWORD':'',
'HOST':'',
'PORT':'',
},
}
You will also have to grant limited permissions for the default runtime access in MySQL:
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'runtime'#'localhost' IDENTIFIED BY 'runtime_password';
Finally when you run syncdb specify your sync access profile:
python manage.py syncdb --database=sync
That should give you the security you want at runtime and the access you want on the command line.