1
votes

I am trying to use local SQL Server database as a default database in django application. I have declared database in setting.py file as below:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'Test',
        'USER': 'sa',
        'PASSWORD': 'P@ssw0rd1234',
        'HOST': 'DAL1281',
        'PORT': '1433',
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
            'host_is_server': True
        },
    },
}

While running the server I am getting below error:

django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10061)')

I am able to connect to database using SSMS. Also I have checked TCP/IP protocol is enabled in SQL Server Configuration Manager.

1

1 Answers

1
votes

Try with this configuration:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': '127.0.0.1',
        'PORT': '',
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PWD',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}