django - use environment variables in debian not working - python

It might be simple thing but I just can't seem to get it running.
in my Django project's settings, I have these db settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PWD'),
'HOST': 'xxxx.yyyyy.eu-central-1.rds.amazonaws.com',
'PORT': '',
}
}
and in /etc/environment I have set the variables and also in .profile.
but this is just not working and I am getting:
(1045, "Access denied for user 'www-data'#'132.31.48.116' (using password: NO)")
If I remove and hardcode the credentials, it works. so obviously my env variables are located in wrong place. can someone please give some look at it?
I am using Debian 4.9.51-1 (2017-09-28) x86_64 in ec2 server

Related

error issue on connecting mysql for django project

I am new to Django.i am learning Django for the past few weeks,i have some trouble on changing the databse to sqllite to mysql.
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': ' ',
'HOST': 'localhost',
'PORT': '3306',
}}
When i try to run the server using py manage.py runserver
It show me the error like
django.db.utils.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
SPECIFICATION
python-3.8
django-3
MySQL(xampp)
OS-windows
This is most likely an issue with your USER, PASSWORD or HOST fields. You are probably not providing correct values. Try executing mysql -u root -p -h localhost and enter the password when asked for it. You should get the same error.
My guess is your password is just empty. So delete the space character for password and it should work.
If that is the case, you should really set a password. Refer to this - How to reset or change the MySQL root password?

Django:permission denied for relation django_migrations, PostgreSql - django user?

I ve been struggling about figuring out relation between postgre users and linux users and the permissions between them.First question I want to ask is that ,let s assume I have tom linux user and tom postgre user which I created with psql command.What is the relation between those two? The reason I am asking is realated with following question:
I am using vagrant and ubuntu in it.There is 2 userss , first one is Linux "vagrant" user , second one is PostgreSql "vagrant" user which I created through following commands:
CREATE USER vagrant WITH PASSWORD '123.123';
GRANT ALL PRIVILEGES ON DATABASE cashier to vagrant;
which should means and also is that vagrant have all permissions on cashier database.
I should also mention as an aside note that I am using provisioning and vagrant uses root user when executing provisions , so root should also have all permissions on the cashier databse because I am importing dump into cashier database on provisioning.
so you can easily assume root have granted permissions through :
CREATE USER root WITH PASSWORD 'vagrant';
GRANT ALL PRIVILEGES ON DATABASE cashier to root;
Here is the problem: When I try to runserver in django python manage.py runserver
if I go with the following settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cashier',
'USER': 'vagrant',
'PASSWORD': '123.123',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
I get authentication error, and if I go with the following settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cashier',
'USER': 'vagrant',
'PASSWORD': 'vagrant',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Then i have Django:permission denied for relation django_migrations, problem.Simply my question is What is going on?What is the relation between users?Which user should i use ? Why django cant migrate with both user credentials?
Recall :
Linux user vagrant pass vagrant
Postgre user vagrant pass 123.123

how do i setup django in wamp?

I want to test my django app in my WAMP server. The idea is that i want to create a web app for aaa.com and aaa.co.uk, if the user enter the domain aaa.co.uk, my django app will serve the UK version, if the user go to aaa.com, the same django app will serve the US version (different frontend). Basically i will be detecting the host of the user and serve the correct templates.
How do i setup my WAMP so i can test this? right now i am using pyCharm default server which is 127.0.0.1:8000
Ok the answer is basically ericeastwood.com/blog/3/django-setup-for-wamp combined with httpd.apache.org/docs/2.4/vhosts/name-based.html – shadow
first install mysqliclient module using below command
pip install mysqlclient
open settings.py file and change database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdbname',
'USER': 'dbusername',
'PASSWORD': 'your password',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}

How to configure settings.DATABASES?

./manage.py syncdb shows some error
(ENV)vijay#vijay-Ideapad-Z570:~/nightybuild/heroku-landing$ ./manage.py syncdb
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
(ENV)vijay#vijay-Ideapad-Z570:~/nightybuild/heroku-landing$
in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
but my app running successfully in heroku. when deploy its automatically heroku postgres confiqured. But in the local development its showing this error .
Can you please tell guide me how to set database for local?
What type of database are you using? Postgres, MySQL, SQLite? That will be your ENGINE value.
Also, what is your local postgres username and password? And your database name?
Below is an example with a postgres database named testdb with the username myusername and password mypassword:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb',
'USER': 'myusername',
'PASSWORD': 'mypassword',
'HOST': '',
'PORT': '',
}
You may need to alter depending on your needs. The biggest change will likely be your ENGINE value. Change postgresql_psycopg2 to mysql if using MySQL or sqlite if using SQLite. Also, if using SQLite, you won't have values for USER and PASSWORD.

Environmental Variables on AWS EC2 using Elasticbeanstalk

I configured a postgressql database on ec2 instance. Now I want to talk to this database server using a different ec2 instance which is running a python/django framework.
My settings.py file contains:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USERNAME'],
'PASSWORD': os.environ['DB_PASSWORD'],
'HOST': os.environ['DB_HOSTNAME'],
'PORT': os.environ['DB_PORT'],
}
}
Where "DB_NAME" and other variables are defined in .ebextensions/*.config file under option_settings. When I push the code to AWS, the deployment log files shows that DB_NAME does not exists. I am not sure where I am going wrong. The Elasticbeanstalk console shows the variables though.

Categories