How to remove backslash in Django DB settings - python

I have been trying to link my mssql database to Django. When I run the Django server I get the error below. For normal query out of Django, it works fine, however, the Django doesnt even connect. My normal username when login into windows is kmoh from EMEA domain, when I normaly login into my windows account it looks like this: EMEA\kmoh but as you can see in the error it looks like this EMEA\\kmoh. So How can I remove this another backslash?
File "C:\ProgramData\Anaconda3\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection
timeout=timeout)
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [28000]
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
I am using these:
Windows 10
django-pyodbc-azure: version 2.1
Django 2.1
Pyodbc: 4.0.25
my Django settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'xxx', #has been hide for this post
'PORT': '1433',
'NAME': 'BBL_Result',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}

I would expect to see the user and password set explicitly in the settings:
DATABASES = {
'default': {
...
'USER': 'db_username',
'PASSWORD': 'db_password',

Related

Authintication problem when accessing MSSQL from Django

I have been trying to link my mssql database to Django. When I run the Django server I get the error below. For normal query out of Django, it works fine, however, the Django doesnt even connect. The weird thing is that when I change the driver to 'SQL SERVER' instead of 'ODBC Driver 17 for SQL Server'. I get no error. However, I need 'ODBC Driver 17 for SQL Server' because it has a lot of features that I need.
File "C:\ProgramData\Anaconda3\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection
timeout=timeout)
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [28000]
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
Windows 10
django-pyodbc-azure: version 2.1
Django 2.1
Pyodbc: 4.0.25
my Django settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'xxx', #has been hide for this post
'PORT': '1433',
'NAME': 'BBL_Result',
#'ODBC_DSN': 'BBL'
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'extra_params': "Persist Security Info=FALSE;server=xxx", #has been hide for this post
},
},
}

How to connect remote SQL server with Django 2

I was trying to connect my Django 2 project with a remote SQL server my connection string is
'default': {
'ENGINE': 'sql_server.pyodbc',
# 'ENGINE': 'django_pyodbc',
'NAME': 'hrm5',
'USER': 'sa',
'PASSWORD': '12345',
'HOST': 'Remote Ip',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
}
I install pyodbc-azure & django-mssql
But still getting this error
django.db.utils.Error: ('01000', "[01000] [unixODBC][Driver
Manager]Can't open lib 'ODBC Driver 11 for SQL Server' : file not
found (0) (SQLDriverConnect)")
Also, I'm using Linux Mint-19 OS and SQL server -v is 12
Your problem is solve here using Django version 2.0.4 and its using this library django-pyodbc-azure 2.0.4.1

Install ODBC Driver heroku

I can not get rid of the error.
django.db.utils.Error: ('01000', "[01000] [unixODBC][Driver
Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not
found (0) (SQLDriverConnect)")
Put
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt
Create file Aptfile.
unixodbc
unixodbc-dev
python-pyodbc
libsqliteodbc
https://packages.microsoft.com/ubuntu/16.04/prod/pool/main/m/msodbcsql/msodbcsql_13.1.9.2-1_amd64.deb
Create file requirements.txt
...
pyodbc
django-pyodbc-azure
settings.py
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
'MARS_Connection': 'True',
}
}
}
How to fix this error I do not know

Django project can't connect with Postgresql database - Is the server running on host “127.0.0.1” and accepting TCP/IP connections on port 5432?

I am using Docker with my Django project. When I am trying to run server docker-compose up I get error:
django_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
django_1 | Is the server running on host "127.0.0.1" and accepting
django_1 | TCP/IP connections on port 5432?
It seems to me that I was trying everything. I have no idea what is wrong. My settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}
I was trying also with such settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
Then I get:
django_1 | django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
I am using postgres app and I start server. There is information that server is running. It seems to me that it should be ok.
My pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
I have changed my docker-compose.yaml and it seems working, but I have new problem. When I do simple GET request I get:
ProgrammingError at /data
relation "authtoken_token" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "authtoken...
When I was working with mysql database everything worked fine.

Connect to a PostgreSQL database on a Docker container

I want to run an application with a PostgreSQL database and a REST API powered by Django on separate Docker containers. So far the API has been running on Docker connecting to a SQLite database, but I'm having trouble now that I want to connect to a PostgreSQL database instead.
Docker's docker-compose.yml file:
version: '2'
services:
postgres:
image: postgres
api:
build: .
command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:1337"
volumes:
- .:/usr/src/app
ports:
- "1337:1337"
depends_on:
- postgres
Django's settings.py (using the default settings which the base postgres image works with according to the documentation):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'wgomanager',
'USER': 'postgres',
'PASSWORD': 'mysecretpassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
When I launch the application with docker-compose up eventually this error is thrown:
api_1 | connection = Database.connect(**conn_params)
api_1 | File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
api_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
api_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
api_1 | Is the server running on host "localhost" (::1) and accepting
api_1 | TCP/IP connections on port 5432?
api_1 | could not connect to server: Connection refused
api_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
api_1 | TCP/IP connections on port 5432?
api_1 |
orchestrator_api_1 exited with code 1
What am I doing wrong?
When using docker-compose, you "discover" services via hostname. Your database service is defined with label postgres. Use it as a hostname in your application cofiguration.
Also password and DB name must be in sync with your app config. This is done via environment variables for postgres service:
services:
postgres:
environment:
- POSTGRES_PASSWORD: "mysecretpassword"
- POSTGRES_DB: "wgomanager"
# rest of docker-compose.yml
Reffer to image docs on how various env. vars affect service configuration.

Resources