django.db.utils.OperationalError: FATAL: database "Path" doesn't exist - django

I'm newbie with python and django,
Im trying to setting up Django in windows7,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'amour'),
'USER': 'openpg',
'PASSWORD': 'openpgpwd',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
when trying to execute the server: python manage.py runserver
the below error appear :django.db.utils.OperationalError: FATAL: database "Path" doesn't exist
I already install PostgreSql 9.3 and Python 2.7.

You should pass the name of the database, not the filename. So if you created database named "amour" then setting will be:
DATABASES = {
'default': {
...
'NAME': 'amour',
...
}
}

Related

Cannot connect to "#localhost". Access denied for user 'root'#'localhost' (using password: YES)

I try to connect mysql to django project
Here my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': 3306,
}
}
enter image description here
Looks like you do not have such user in your database.
Please use that command in terminal, while being inside of your app's main folder:
python manage.py createsuperuser

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 always connects to localhost mongodb

I have followed the guide of db connection config:
https://nesdis.github.io/djongo/database-configuration/
However, it always connects to localhost one, not my setting's one.
Does anyone have any idea on this issue?
my packages versions:
Django 2.0
django-cors-headers 2.4.0
django-rest-auth 0.9.3
djangorestframework 3.9.0
djongo 1.1
mongoengine 0.16.3
pip 10.0.1
pymongo 3.7.2
urllib3 1.24.1
my settings
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'test_db',
'HOST': 'somewhere.com',
'PORT': 27017
}
}
Is seems that somewhere along the way, djongo changed the structure of the database settings. After wasting days trying to find the solution, I came across a page that had the updated structure, Try setting your DATABASE structure to this:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'yourmongodb',
'CLIENT': {
'host': 'some-host.or.ip',
'port': 27017,
'username': 'youruser',
'password': 'yourdbpass',
'authSource': 'yourcollection',
}
},
}
Have you edited your setting in the settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'XXXX',
'USER': 'XXX',
'PASSWORD': 'XXXX',
'HOST': 'XXX.XXX.XXX.XXX',
'PORT': '3306',
'OPTION': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
First: Create a SSH Tunnel to remote mysql server on localhost
ssh -N -L 3306:127.0.0.1:3306 root#192.168.0.122
192.168.0.122 >> is a remote LAN server of mine.
Second : use 127.0.0.1 as a HOST on Django connection string.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'XXXX',
'USER': 'XXX',
'PASSWORD': 'XXXX',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTION': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
OR change HOST to 127.0.0.1 in .env if using environment file.
This worked for me.

How to add a remote postgres database in my django app?

What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.

Database configuration in Django

How do I use a database connection URL in setting up database config in Django as opposed to using a dictionary?
Instead of using:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'PORT': os.getenv('DB_PORT'),
'HOST': os.getenv('DB_HOST')
}
}
I want to use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgresql://DB_USER:DB_PASSWORD#localhost:5432/DB_NAME'
}
}
This is how I added a database url to one of my django apps.
First, install this package
pip install dj-database-url
Next, add this line to your settings.py in your django project
...
DATABASES['default'] = dj_database_url.config(default=os.getenv('DATABASE_URI'))
...
In place of os.getenv('DATABASE_URI'), you can also explicitly mention your database url
You can refer here to the github repo for more configurations.