Django Connection not working properly with Azure Active Directory - django

I'm trying to put my application out in production. It was fully working when I was working locally, and it also worked when I did it in a test database in azure.
The problem is that now, when I want to use the actual production database, it is sending me errors.
I'm pretty sure that I have all the settings correct, but I think this is something I have to change inside my Azure settings, but I can't find out why.
This is my connection:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'Reconciliation',
'USER': 'mymail#mycompany.net',
'PASSWORD': 'pa$$words$$$.',
'HOST': 'mycompany.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
This is the error I'm getting:
[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open server "myserver" requested by the login. The login failed. (40532); [HY000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)')
Should I be changing some settings inside Azure for it to work?

The default was correct. I had to add this so it worked:
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
"extra_params": 'Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword',
},
The options will depend on the service you are using. I went into the azure portal and there they specified which where this extra parameters I needed to add.

Related

while connecting to MicrosoftSQL server using Django facing django.db.utils.OperationalError:

drivers available with me
**python shell**
'''In [2]: pyodbc.drivers()'''
**Output:**
**Out[2]: ['SQL Server']**
code in settings.py django:
**Settings.py in django**
'''# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbname',
'HOST': 'ansqlserver.database.windows.net',
'USER': 'test',
'PASSWORD': 'Password',
'OPTIONS': {
'driver': 'SQL Server',
}
}'''
**ERROR:**
**Trying to connect to MicrsoftSQL server facing below error**
File "C:\Local\Programs\Python\Python37\lib\site-packages\sql_server\pyodbc\base.py", line 314,
in get_new_connectiontimeout=timeout)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSNnor SERVER keyword supplied (0) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
Check if your database host "ansqlserver.database.windows.net" has permission to allow application server to connect.
Initally I have only one driver: 'SQL Server'
later I installed different drivers
Just enter command and see the drivers in your machine
'''pyodbc.drivers()'''
Output: ['SQL Server',
'ODBC Driver 17 for SQL Server',
'ODBC Driver 13 for SQL Server']
after that I am able to connect to MicrosoftSQL DB without any issues

Django Unit tests fail over domain socket

I have Django configured to use the database with peer authentication over the local Unix Domain socket, instead of user/password authentication.
Here's the settings.DATABASES:
{'default': {'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mcps',
'PORT': 5433,
'TEST': {'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mytestdb',
'PORT': 5433,
'USER': 'mcp'},
'USER': 'mcp'}
}
The port is correctly configured, the application itself has no problem working correctly.
Yet, when I try to run pytest, with the environment variable DJANGO_SETTINGS_MODULE set to the above settings, a database is created - with the correct owner 'mcp' - but before tables are created I get an error:
django.db.utils.OperationalError: FATAL: Peer authentication failed for user "mcp"
What are unit tests doing differently anmd how can I fix this please?
#Nader Alexan There's no host to set, communication goes over the local Unix domain socket. I tried adding
'HOST': '/run/postgresql'
as I had to do in pgAdmin, but that didn't solve the issue.
It turns out that pytest also tries to access the database named 'postgres', even though the maintenance database is set to template1. I haven't been able to determine why, but adding access to that database explictely in pg_hba.conf enabled me to run the tests.
Sorry if this explanation is a bit shaky, I'm new to forms of PG authentication other than username/password myself.

Django connect to MSSQL

Hi I am trying to connect from django to mssql server. I installed
1 : pip install django-mssql
2 : pip install pywin32
after that i changed the database inside setting.py =>
DATABASES = {
'default': {
'NAME': 'TMSWEB',
'ENGINE': 'sql_server.pyodbc',
'HOST': '192.168.72.1\MSSQLSERVER5',
'USER': 'sa',
'PASSWORD': 'pwd',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
}
}
}
and then i check like that=>
2: python manage.py makemigrations
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC
Driver Manager] Data source name not found and no default driver
specified (0) (SQLDriverConnect)')
What i am still need to do? Please help me.
my django version : 2.1.11
This is because of I don't have ODBC Driver 13 for SQL Server. I download from ODBC Driver 13 for sql server and installed. Its fixed. Thanks.

Access denied when connecting Django to RDS via SSL

I'm trying to connect Django to AWS Aurora server(RDS Aurora 5.6.10a) but I'm getting
django.db.utils.OperationalError: (1045, "Access denied for user 'admin'#'xx.xx.xx.xx' (using password: YES)
In Django settings.py, I've used the following settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': MYSQL_DATABASE,
'USER': MYSQL_USER,
'PASSWORD': MYSQL_PASSWORD,
'HOST': MYSQL_SERVER,
'PORT': '3306',
'OPTIONS': {
'ssl': {
'ca': '/Users/mac-user/workspace/project/settings/rds-combined-ca-bundle.pem',
}
}
}
}
I've also looked at similar questions like this but didn't work
I've also tried to connect using mysql-connector-python and mysql.connector.django as database engine, and ensured that I'm setting the right configuration (setting OPTIONS's ssl_ca and ssl_verify_cert) but I've also got a similar error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'admin'#'xx.xx.xx.xx' (using password: YES)
I've tried to connect to the database using PHP and command line and it worked fine:
mysql -h '<host>' -u '<username>' -p'<password>' --ssl-ca='<SSL_certificate_absolute_path>'
Am I missing any configuration for Django?
I don't have a direct answer to your problem, but apparently Aurora does not respond meaningfully (or correctly?) when MySQL client does not have ssl enabled (like MySQL command line and your PHP solution).
So my advice is to somehow force the MySQL client used by Django to use SSL connection, apparently it's not enabled/forced by default (the options you used might be insufficient).

Postgresql not connecting in django project

Working on django project with postgresql database but its giving me error :-
psql: FATAL: no pg_hba.conf entry for host "::1", user "dev_onpoint", database "dev_mypsdb", SSL off
My database configuration in django is :-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dev_mypsdb',
'USER': 'dev_onpoint',
'PASSWORD': '*****',
'HOST': 'localhost',
'PORT': '5432'
}
}
I also checked in postgresql configuration file for port and found that i am using right port number.
Then i checked ph_hba.conf file but because I am not familiar with postgresql database and I am using it first time, that's why I could not understand this file :-
enter image description here
Please let me know if i skipped anything in configuration.
Thanks
Your pg_hba.conf file has no access control entry for IPv6 connections, that's why connect from ::1 fails. You can add the following line to your pg_hba.conf:
host all all samehost md5
to allow connections from all local IPs using password authentication (you must restart postgresql after editing this file). For more information refer to the postgres documentation.