I have a problem with Django database configuration with 'postgresql_psycopg2'
If I give any arbitrary name to my database like below,
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'xyz', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': '${my_password}', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
"autocommit": True,
}
}
}
>
OperationalError at /
FATAL: database "xyz" does not exist
I surf a lot and findout same that with SQLite we have to specify absolute path to our database; with PostGRE likewise above.
I would like to know:
1) Why I am getting error message with above specification and
2) How I use my database which i am using with Development server lay out in filesystem (windows).
I surf a lot and findout same that
with SQLite we have to specify
absolute path to our database; with
PostGRE likewise above.
Just to be clear (your wording isn't) with postgresql there is no absolute path, it's just the name of the database.
1: The error message is pretty clear, database xyz does not exist.
Go to the dbshell (python manage.py dbshell) and type in \l. If database xyz isn't in there, type in create database xyz, exit the postgres shell and try syncdb again.
2: For your development server, I recommend using sqlite3 for its ease of use.
sqlite is included in python 2.5+, so no extra settings are required. Simply set your ENGINE to sqlite3, specify an absolute path to where you want the database saved, and python manage.py syncdb
Related
I am new to both GKE and Django. I made an app in Django, made a docker container and push it to gcr and deploy it via GKE. The deployment works fine but when i try to login, I got the OperationalError. For database connection, I am using CloudSQL proxy.I have collected the static file and stored in google storage. Any help will be highly appreciated.
I have tried quite many opinions available already online but failed to succeed.
When i try to login as admin, I got the following output after input my username and password for login.
OperationalError at /admin/login
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Following are my database setting in Django.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'polls',
'USER': os.getenv('DATABASE_USER'),
'PASSWORD': os.getenv('DATABASE_PASSWORD'),
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
error while trying to login as admin
You should check in the docker logs , and check if there is any error connecting to the database
If it is a databse connection issue, then you can try the following in your docker-compose.yml . You can customize the rest of the variables mentioned, as needed for your polls application
you can try this
web:
build: ./app
image: {imagename}
depends_on:
- cloud-sql-proxy
environment:
- SQL_ENGINE=django.db.backends.postgresql_psycopg2
- SQL_DATABASE=test_db
- SQL_USER=postgres1
- SQL_PASSWORD=6728298
- SQL_HOST=cloud-sql-proxy
- SQL_PORT=5432
- DATABASE=postgres
cloud-sql-proxy:
image: gcr.io/cloudsql-docker/gce-proxy:1.11
command: /cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:0.0.0.0:5432 -credential_file=/config
volumes:
- {service_account_creds_path.json}:/config
You could read this article https://adilsoncarvalho.com/how-to-use-cloud-sql-proxy-on-docker-compose-f7418c53eed9 for reference. The article is about mysql, but the concepts are the same . Good Luck!
I am struggling with connecting to my local MsSQL server with Django. It says when I make command 'makemigrations':
django.core.exceptions.ImproperlyConfigured: 'sqlserver_ado' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My settings:
DATABASES = {
'default': {
'NAME': 'TestDB',
'ENGINE': 'sqlserver_ado',
'HOST': 'STEPAN',
'USER': '',
'PASSWORD': '',
}
}
In this solution: Setting up django-mssql issues I found that my 'sqlserver_ado' should be in the same folder 'site-packages', which it is (Python IDLE shell):
>>> import sqlserver_ado
>>> sqlserver_ado
<module 'sqlserver_ado' from 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages\\sqlserver_ado\\__init__.py'>
>>> import django
>>> django
<module 'django' from 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages\\django\\__init__.py'>
I have a PyWin32 installed and importing in IDLE is fine (even the path is fine), yet still I get this error message.
Any ideas or advices?
Spec: Win10, Python 3.6, Visual Studio 2017, django-mssql 1.8, django 2.0.6, MsSQL (developer) and this DB is present (with flask I have no problem to connect to my local server [just for comparsion my connection string in another flask project:
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc:///?odbc_connect={}".format(urllib.parse.quote_plus(
"DRIVER={SQL Server};Server=STEPAN;Database=TestDB;Trusted_Connection=yes;"))
]), project was automaticly created by Visual Studio, command that throws this error:
python manage.py makemigrations
Responding to an old post, but - in case this will help others:
I found that, in some cases, you need to specify the SQL Server driver version too:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<dbNAME_HERE',
'HOST': '<SERVERNAME_HERE>\SQLEXPRESS',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
}
}
}
This is for a DJANGO project, but the issue may be similar...
I want to use SQL Server as the backend for Django.
I install pyodbc, django-pyodbc, django-pyodbc-azure
In settings.py I replace a Database
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': 'xxx.database.windows.net',
'PORT': '1433',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
'MARS_Connection': 'True',
}
}
}
if I run python manage.py syncdb
Error:
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend. Available options are: 'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sql_server', 'sqlite3'
Error was: No module named sql_server.pyodbc.base
I use Visual Studio + Python 2.7
You may want to follow that documentation https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-ptvs-django-sql/. The Python tools for Visual Studio makes things easier; still, even if you don't use them, the documentation will show you how they do, how they install pyodbc locally, then push it to the server while publishing.
Note that while Azure has 64 bit VMs, the Python environment must be 32 bits.
I want to build a site where users can log in register and that stuff. For the User management i use FOSUserbundle. Now i want to use a different db connection for FOSUserBundle than for the other bundles. My config.yml file looks like:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database1_driver%"
host: "%database1_host%"
port: "%database1_port%"
dbname: "§database1_name%"
user: "%database1_user%"
password: "%database1_password%"
charset: UTF8
user:
driver: "%database2_driver%"
host: "%database2_host%"
port: "%database2_port%"
dbname: "%database2_name%"
user: "%database2_user%"
password: "%database2_password%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyProjectMainBundle: ~
user:
connection: user
mappings:
MyProjectUserBundle: ~
When i try to load the page i get the error MappingException: The class 'MyProject\UserBundle\Entity\User' was not found in the chain configured namespaces MyProject\MainBundle\Entity, FOS\UserBundle\Model.
I followed the documentation for FOSUserBundle exactly and it is working if i use
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
in config.yml.
The only thing with this is, it only generates the table like defined in the new entity from the documentation. Only an id field is generated, and not the whole fos_user table like it should.
I know some similar questions have been asked before, but I tried using all the solutions from there and it didn't work. So how can I fix this? Is it even possible? I really need to use seperate databases because my project will use a lot of tables and i don't want it to get too messy.
user:
connection: user
mappings:
FOSUserBundle: ~
MyProjectUserBundle: ~
Need to add the FOSUserBundle to your mappings to get rid of the entity error
And make sure you have the model_manager_name set in config.ym;
fos_user:
db_driver: orm
firewall_name: main
user_class: Cerad\Bundle\AccountBundle\Entity\AccountUser
model_manager_name: user
Another developer and I are setting up a django (v1.4.2) project using a legacy SQL server database (SQLEXPRESS) on another server. So far, we have been able to connect to the database from linux and mac using django-pyodbc, and from a laptop running windows 7 using django-mssql. I would like to use django-pyodbc on the laptop to keep the environments in sync.
On the laptop:
pyodbc (3.0.6) is installed and in a non-django .py script I can connect and run sql statements
Downloaded django-pyodbc 1.4 by downloading the zip; I'm not sure I installed it right:
I unzipped the file, and ran the setup.py file in the top directory; it puts a sql_server directory in the /lib/site-packages directory
Copied this sql_server directory to /django/db/backends
Created a PYTHONPATH environment variable pointing to /django/db/backends/sql_server
not sure if it's supposed to point to /site-packages/sql_server instead?
Created an ODBC Data Source (System DSN)
testing the connection option works
Editted the DATABASE entry in settings.py to be almost exactly like the linux version (details below)
So, it doesn't work, and I get the following error message, and have no idea what to do next:
('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
I setup the django settings.py file as like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sql_server.pyodbc',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': 'mssqlx',
'PORT': '12345',
'OPTIONS': {
'driver': 'SQL Server',
},
},
}
On linux, my settings file has a DATABASES entry like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sql_server.pyodbc',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': 'mssqlx', # ODBC DSN defined in /etc/freetds.conf
'PORT': '12345', # Probably unneeded. Set in mssqlx
'OPTIONS': {
'driver': 'SQL Server', # ODBC driver name in /etc/odbcinst.ini
'extra_params': "TDS_VERSION=7.0" # Probably unneeded. Set in mssqlx
}
},
}
don't know if it will help solve this, but using django-mssql (which only runs on windows), the (working) entry is:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlserver_ado',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': '199.555.0.10', # changed for this example
'PORT': '12345',
'OPTIONS': {'provider': 'SQLOLEDB'}
},
}
Don't know what other info might help. Thank you for any help or insight you can offer.
----POST MORTEM ----
Here's what finally worked:
partial entry in settings for DATABASES:
'default': {
'ENGINE' : 'django.db.backends.sql_server.pyodbc',
'NAME' : 'test_db_name',
'USER' : 'test_db_user_name',
'PASSWORD' : 'password',
# ODBC DSN defined in /etc/freetds.conf
'HOST' : 'mssql_test',
# Ignored for Windows; Required for Linux
'OPTIONS' : {
# ODBC driver name in /etc/odbcinst.ini
'driver': 'SQL Server',
# NOTE: dsn option is added dynamically later, for Windows
}
},
# The ODBC DSN name specified above as DATABASES.default.HOST is ignored on
# Windows, where it must be specified as DATABASES.default.OPTIONS.dsn instead.
# However, we haven't found a way to make DATABASES.default.OPTIONS.dsn work in
# Linux (and probably the same for Mac). It causes the error:
# Data source name not found, and no default driver specified
# Therefore we add it here, but only for Windows.
# Note: The username and pwd in the windows dsn file is apparently NOT used
# (b/c server hosts both test and prod database in same MSSQL
# instance, both test and prod dsn files happen to work - they have the
# same ip address and port number, but different username/password's)
#
# On 64-bit Windows, with our current 32-bit version of pyodbc, the DSN
# must be created via:
# C:\Windows\SysWOW64\odbcad32.exe
# instead of the regular "ODBC Data Sources" app in Control Panel, which
# invokes:
# C:\Windows\system32\odbcad32.exe
#
# os.name is...
# nt for Hans' laptop (Windows 7)
# posix for the "Amazon Linux AMI" (CentOS) on AWS
# posix for Fred's Mac
if os.name == 'nt': # Windows
DATABASES['cf']['OPTIONS']['dsn'] = 'mssql_test'
Try using https://github.com/michiya/django-pyodbc-azure. This should work on both Linux and Windows.
Then define your database settings as such:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbname',
'HOST': 'dsn_entry',
'PORT': 'port',
'USER': '',
'PASSWORD': 'pass',
'OPTIONS': {
'driver': 'FreeTDS',
'dsn': 'dsn_entry',
'host_is_server': True
}
}
}
Under Windows the 'driver' entry in OPTIONS should be:
'driver': 'SQL Native Client',
Edit: Oops, failed to see that the you had solved the problem. Leaving my answer here as reference.