How do I get Jython-Django to work with Postgresql JDBC - django

I have tried to setup an instance of Django on Jython using Postgresql. I can't get Django to talk to Postgresql. Can someone help me figure out what is wrong?**
So far, I have the following system installed for development:
Windows 7
JDK7 (x86)
Jython Version 2.7b2
Django-1.6.5
django-jython 1.7.0b2(Database backends and management commands, for development underDjango/Jython)
postgresql-9.3.5-1-windows-x64.exe
Eclipse-PyDev
I was able to run the server and database, but now I am having issues with configuring Django to work with the Postgresql database.
I followed the configuration steps here:
https://pythonhosted.org/django-jython/database-backends.html#postgresql
My settings.py file has the following:
DATABASES = {
'default': {
'ENGINE': 'doj.db.backends.postgresql',
'NAME': 'gate',
'USER': 'gate',
'PASSWORD': 'test',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
When I check my class path:
C:\>echo %CLASSPATH%
C:\Program Files (x86)\Java\jdk1.7.0_67\bin.;C:\Program Files (x86)\Java\jre7\li
b\ext\QTJava.zip
I have the JDBC file postgresql-9.3-1102.jdbc41.jar in the lib folder listed above.
Error Message from Django
File "C:\jython2.7b2\Lib\site-packages\django\db\utils.py", line 131, in load_backend raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'doj.db.backends.postgresql' isn't an available
database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named schema

I believe the problem is that you are using the wrong version of Django.
Jython-Django is compatible only starting from 1.7.
See https://docs.djangoproject.com/en/1.6/howto/jython/

Related

Test database for Django + Heroku. Error creating the test database: permission denied to create database

I'm trying to run the tests for my Django project. I wrote this project some time ago, I had different settings then and tests were passing. Now I changed settings and deployed it on Heroku with Heroku Postgres database. Everything works fine already except I can't run tests. I've tried many different settings and nothing worked. Most of the time I'm getting this error: permission denied to create database
My last setting is following the instruction from this article on medium
Basically I have added 2nd Heroku Postgres database, add settings like below (but with valid variables of my heroku databases):
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7osdssadag0ugv5',
'USER': 'lhwwasadqlgjra',
'PASSWORD': '1524f48a2ce41177c4ssdadasd3a11680b735302d14979d312ff36',
'HOST': 'ec2-54-75-2326-118.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7osdssadag0ugv5',
}
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7hasadas9hqts5',
'USER': 'nybkduadsdgqot',
'PASSWORD': 'bb535b9cdsfsdfdsfdsfac54851f267444dd8cc230b2a786ab9f446',
'HOST': 'ec2-54-247-132-38.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7hasadas9hqts5',
}
}
}
Then run python manage.py test --keepdb in my venv. Then I get an error:
RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
I have also tried what is advised in this article
Do you have any ideas what I could do about this error? I don't know Django well. I play with it from time to time.
I'm using: Python 3.6.9, Django 3.0.3, Heroku Postgresql Hobby Dev
EDIT:
I'm not sure if this is now an issue with my settings DATABASES.
Now when I commented out all my settings concerning DATABASES and I run python manage.py runserver my development server starts as normal and I have access to a database I set before (even after restarting a computer). This looks like actual settings don't have effect (??) Any thoughts?
Django version 3.0.3, using settings 'forumproject.settings'
Starting development server at http://127.0.0.1:8000/
Ok, I found out what it was. My database settings were not taken into account, even I had DEBUG=True because I had this line on the end of the settings:
# Activate Django-Heroku.
django_heroku.settings(locals())
After commenting this out the error permission denied to create database
goes away and I can run tests with
python manage.py test --keepdb
I'm surprised how always posting a question on stack overflow help me to find answer immediately after. I was running in circles

Having hard time deploying Django + PostGis on AWS Beanstalk (DatabaseOperations error)

I’m having troubles making Django + PostGIS work on AWS BeanStalk.
The app runs, some parts of the app work properly, but when trying to access object with GeoDjango fields (PointField) I get this error:
Attribute Error
'DatabaseOperations' object has no attribute 'select'
/opt/python/run/venv/lib/python3.4/site-packages/django/contrib/gis/db/models/fields.py
in select_format, line 61
I have googled this error, and very few people encountered it. The closest error I could find is: Getting 'DatabaseOperations' object has no attribute 'geo_db_type' error when doing a syncdb
But my databases is configured correctly in the Django Settings Module as follows:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
I’m using Django 1.9.5 with PostGis 2.3.0 and PostgreSQL 9.5.2
I’m using a custom AMI based on 2017.03 that I made using this script:
https://gist.github.com/nitrag/932ecd7d109f9d1e3ed378353e0d5c2f
PostGis is installing properly on the EC2 instance, I even tried another script and got the same final error. (http://www.daveoncode.com/2014/02/18/creating-custom-ami-with-postgis-and-its-dependencies-to-deploy-django-geodjango-amazon-elastic-beanstalk/)
I am really on my last legs here, any help will be greatly appreciated!

Upload Django app and set database correctly

Hi I want to upload my django app to openshift rhc with git. After push and refresh the mainpage is displayed, however everything which needs user instance is not working.
My error is:
'Ident authentication failed for user "admin"'
...py2.6.egg/django/db/backends/postgresql_psycopg2/base.py in _cursor, line 177
I think database is not connected properly:
in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'MY_APP_NAME',
'USER': 'admin',
'PASSWORD': 'MY_PASSWORD',
'HOST': '',
'PORT': '',
}
I didn't do manage.py syncdb, I do not know how to do it. Maybe that is the problem, because superuser is not created ?
What about path to db? On my computer settings.py looks a litle bit different:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '//home//pachucx//Project//db//sqlite3.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
Everything worked fine on the computer.
The difference is that in NAME I am using path to the DB file, where on serwer side I use only name of it. Should I add extension .db, or maybe there is need to add a path I dont know.
Or maybe the problem will be solved just by manage.py syncdb? If it is so, tell me how to do it properly e.g with git
Many thanks.
This is an error that is occurring because of your attempt to access the database that is either not created, or you have invalid username and password credentials in your settings file.
Make sure you configure all the settings correctly. - Check your host, db username and password as well as the port - it could be different from your local box.
In order to sync the database you need to navigate in SSH to your project root and locate the manage.py file. Once you have that run the following command:
python manage.py syncdb
This will either:
Create all the tables,
Give you a nasty error saying your settings for your database are not correct, this is a great error to get as you know you need to look into the settings again and correct the problem.
To test whether or not your server can be initiated run the following command in SSH again
python manage.py runserver
If successful, this will give you a local test environment and should spit out a url to test. Should be localhost:8000 or something similar.
If fails, this will let you know if you have models improperly configured, mainly your settings, or url file it will give an error on so you can double check everything is up.
As for using the database that is on your machine on a machine outside of your network I advise to not do that. Go and create the database on the box you have, and make notes of the host, username, password, database name etc, and go back to start of this answer.
All the best,

Django MongoDB Engine connection failure

[SOLVED] After successfully completing the django tutorial, I have tried to use mongoDB as a database, with Django MongoDB Engine. This is the database configuration in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'test',
'HOST': 'localhost',
'PORT': 27017,
# 'OPTIONS' : {
# 'slave_okay' : True,
# }
}
}
And this is the error message I get, after running python manage.py syncdb
:
raise AutoReconnect("could not find master/primary")
pymongo.errors.AutoReconnect: could not find master/primary
I got this error, even after shutting down the mongoDB server, so I figured out that this needed to be a connection issue. I posted an answer on how I solved this.
Using Ubuntu 12.04 LTS x64, Python 2.7, django 1.4.2, mongoDB x86_64 2.2.1 (clean new install)
The way I solved this is so simple that I am almost ashamed at myself for asking the question in the first place, but I hope it will be useful:
I simply upgraded the Django MongoDB Engine by typing into the terminal:
sudo pip install git+https://github.com/django-nonrel/mongodb-engine --upgrade
And that's it.
Can you try running ...
db.repairDatabase()
... from the mongo terminal?

Setting up django-mssql issues

I'm having some issues setting up django-mssql on Win Server 2008 R2. I have everything installed, however, the wiki for django-mssql says to setup the settings file similar to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
When I run from my site directory:
python manage.py syncdb
I get an error stating it isn't an available database backend. When I installed django-mssql it seemed to install the backend here \site-packages\django_mssql-1.0.1-py2.7.egg\sqlserver_ado does this need to be copied to site-packages\django\db\backends?
I get the same error if I set my settings to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'django_mssql-1.0.1-py2.7.egg.sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
Am I missing something when setting up this backend? This is my first time using django, but I didn't see anything in the documentation for setting up a different backend, and the django-mssql wiki or issues doesn't seem to have anything either.
Also, if there is other documentation somewhere that can help please let me know.
EDIT: The django app is running on Ubuntu server.
Dustin's comment about making sure "import sqlserver_ado" from the command shell got me going down the right path on my Django 1.8.1, Python 3.5 Win32 system with pywin32 installed.
SPOILER ALERT This only gets my Django instance to run without errors. I haven't tested the ADO connection yet.
The first error message I got was:
No module named 'django.db.backends.util'
and I found there is a file called: django.db.backends.utils so I copied it and renamed it to django.db.backends.util (without the 's') and away went the error message!
So hoping this wasn't too harmful, I continued on this line of troubleshooting.
The next error message I got was:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 7, in <module>
from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
ImportError: cannot import name 'BaseDatabaseWrapper'
I changed line 7 in base.py to now say:
#from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.base.validation import BaseDatabaseValidation
from django.db.backends.base.client import BaseDatabaseClient
Yes, that's commenting out the bad line and adding four separate lines.
Then I got this error:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 18, in <module>
from .introspection import DatabaseIntrospection
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\introspection.py", line 3, in
from django.db.backends import BaseDatabaseIntrospection
ImportError: cannot import name 'BaseDatabaseIntrospection'
so I changed the line 3 to now read:
from django.db.backends.base.introspection import BaseDatabaseIntrospection
and so on for creation.py:
from django.db.backends.base.creation import BaseDatabaseCreation
for operations.py:
from django.db.backends.base.operations import BaseDatabaseOperations
for schema.py:
from django.utils.log import getLogger
Hope this helps someone. Hope the ADO module actually connects to something.
-Sean
You will want to make sure that you can import "sqlserver_ado" from your python shell.
Put the folder sqlserver_ado somewhere on your PATH, I put mine in \site-packages\
Take a look at the README.txt.
The engine does want to be set to "sqlserver_ado" similar to how the settings are done on the settings sample page.
As of 2019:
I couldn't get Django MSSQL to work at all.
I switched over to django-pyodbc-azure and that works great.
Install:
pip install django-pyodbc-azure
Setup:
'ENGINE': 'sql_server.pyodbc'
You need to install the dependency PyWin32. You can install via pip or download from the python binaries page http://www.lfd.uci.edu/~gohlke/pythonlibs/
I was trying to get django_pyodbc to work, and couldn't. I was getting this error:
django.core.exceptions.ImproperlyConfigured: 'django_pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper
I was directed to this post as a solution, so I'll post my answer here also.
I downgraded to django 1.6 instead of 1.8, and now django_pyodbc works as a database backend.
As per https://github.com/lionheart/django-pyodbc/pull/96, django_pyodbc should now work with Django 1.8. So this seems to be a good alternative to django-mssql for those requiring SQL Server 2008 R2 support.