Django error on connecting to Postgres, unsupported startup parameter: options - django

On migrate step I get such error django.db.utils.OperationalError: ERROR: unsupported startup parameter: options
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'OPTIONS': {
'options': '-c search_path=public'
},
'NAME': env.str('DB_NAME'),
'USER': env.str('DB_USER'),
'PASSWORD': env.str('DB_PASS'),
'HOST': env.str('DB_HOST'),
'PORT': env.str('DB_PORT'),
'DISABLE_SERVER_SIDE_CURSORS': True,
}
}
(in options parameter I tried to set default schema to public)
Can this error appear because I have pgbouncer?

Related

connect SQL Server in django rather than default database

How to connect SQL Server in django rather than default database? What do I have to write in "settings.py" file?
You actually can install package for mssql:
pip install mssql-django
Then change your settings.py:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'mydb',
'USER': 'user#myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
And btw you can turn off pyodbc's connection pooling by adding this in your settings:
DATABASE_CONNECTION_POOLING = False

Django not connecting to PlanetScale , SSL error

Trying to use planetscale for my db platform for a Django app that i am building. However i'm running into some errors
django.db.utils.OperationalError: (2026, "SSL connection error: no valid certificates were found, CAFile='*************', CAPath=''. One or more of the parameters passed to the function was invalid. Error 2148074333/0x8009035D")
The configuration was copied straight from planetscale
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': env('DB_NAME'),
'HOST': env('DB_HOST'),
'PORT': env('DB_PORT'),
'USER': env('DB_USER'),
'PASSWORD': env('DB_PASSWORD'),
'OPTIONS': {'ssl': {'ca': env('MYSQL_ATTR_SSL_CA')}}
}
}
Finally got it working.
the value for the OPTIONS key should be {'ssl': {'ssl-ca': env('MYSQL_ATTR_SSL_CA')}}

SyntaxError: EOL while scanning string literal (I am trying to connect Django with PostgreSQL)

I am trying to connect Django to the PostgreSQL database, but this is my first time doing it, so I am not sure how exactly to do it. The problem seems to be with the line, where the password is.
settings.py -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<db_name>',
'USER': '<db_username>',
'PASSWORD: '<password>',
'HOST': 'db_hostname_or_ip>',
'PORT': 'db_port',
}
}
When I try to make migrations in my prompt, (python manage.py makemigrations) I am getting the error mentioned in the title. Can anybody help me please?
Your code
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<db_name>',
'USER': '<db_username>',
'PASSWORD: '<password>',
'HOST': 'db_hostname_or_ip>',
'PORT': 'db_port',
}
}
What it actually should be
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<db_name>',
'USER': '<db_username>',
'PASSWORD': '<password>',
'HOST': 'db_hostname_or_ip>',
'PORT': 'db_port',
}
}
Actually you're forgetting the closing qoute after PASSWORD

Getting a 'settings.DATABASES is improperly configured. Please supply the NAME value' error

I am using Django to connect to an on-premise database. Earlier, the database was hosted on Azure.
The connection string I used within Django settings earlier was as follows-
for sql database of Azure
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DatabaseName', #notrealname
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'sql-django-uat.database.windows.net', #notreal
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
'MARS_Connection': 'True',
}
After the database migration, this string doesnt work. I keep getting 'Login timeout expired'.
But substituting 'NAME' with "DATABASE' works. Example given below-
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'DATABASE': 'DatabaseName', #notrealname
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'on-prem.local',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
'MARS_Connection': 'True',
}
My webapp gets to the login page. But after I log in, I get the error- 'ImproperlyConfigured at /login/
settings.DATABASES is improperly configured. Please supply the NAME value.'
Can someone tell me how to solve this? I should mention leaving the "NAME" field blank also gives the same error. Thanks so much in advance.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DatabaseName', #notrealname
'USER': '<replace with on-premise DB Username>',
'PASSWORD': '<replace with on-premise DB password>',
'HOST': '<replace with database URL of on-premise DB URL>',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
'MARS_Connection': 'True',
}
From the Azure SQL database, the settings.py should be:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydb',
'USER': 'user#myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = False
USER: String. Database user name in "user" (on-premise) or "user#server" (Azure SQL Database) format. If not given then MS Integrated Security will be used.
Reference: django-pyodbc-azure 2.1.0.0
For on-premise SQL server, the database configuration:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': '(LocalDB)\ProjectLocalDB',
'PORT': '',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_password',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
Here are two tutorials, I think you can learn from them to check if you have missed something:
How to use Django with SQL Server LocalDB: This post has been
tested with Microsoft SQL Server 2017, Django 1.11.x and 2.x. The
pyodbc and django-pyodbc-azure packages will be used to connect
Django to SQL Server. The version of django-pyodbc-azure must match
your version of Django.
Django and MS SQL Server: The new python module/library is the django-pyodbc-azure which supports Django
2.0 and lower versions like Django 1.11.
Hope this helps.

django-pyodbc and calling a stored procedure

I'm testing my code on Windows 10. I have a Django application that needs to call a stored procedure on a remote SQL Server database. Here's the DATABASES snippet from settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'HOST': 'mycompany.com',
'PORT': '3306',
'USER': 'user',
'PASSWORD': 'pw',
},
'ss': {
'ENGINE': 'django_pyodbc',
'NAME': 'db2',
'HOST': 'myserver\SQLEXPRESS',
'USER': 'myuser',
'PASSWORD': 'mypw',
'PORT': '1433',
# 'DRIVER': 'SQL Server',
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True, # must be True for remote db
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0',
},
},
}
Here's a code snippet from my view:
cursor = connections['ss'].cursor()
cursor.execute("{call dbo.mysproc(?)}", (id))
When I execute the cursor.execute statement I get this error:
django.db.utils.DatabaseError: ('The SQL contains 1 parameter markers,
but 36 parameters were supplied', 'HY000')
My parameter, id, is a GUID.
Thoughts?
Here's the fix, simply changed the parentheses surrounding the parameter to square brackets:
cursor.execute("{call dbo.mysproc(?)}", [id])
I found this by trial and error.