Using postgresql for Django under win7? - python

I am currently playing with django and want to use as my database postgresql:
and these are my configurations in my settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'postgres', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
I also tried it with postgresql_psycopg2 and also installed psycopg2(http://www.stickpeople.com/projects/python/win-psycopg/).
Any ideas whats wrong with the configurations?
UPDATE:
The Error:
> File
> "C:\Python27\lib\site-packages\django\core\management\__init__.py",
> line 382, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\lib\site-packages\django\core\management\base.py", line
> 196, in run_from_argv
> self.execute(*args, **options.__dict__) File "C:\Python27\lib\site-packages\django\core\management\base.py", line
> 232, in execute
> output = self.handle(*args, **options) File "C:\Python27\lib\site-packages\django\core\management\base.py", line
> 371, in handle
> return self.handle_noargs(**options) File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py",
> line 57, in handle_noargs
> cursor = connection.cursor() File "C:\Python27\lib\site-packages\django\db\backends\dummy\base.py", line
> 15, in complain
> raise ImproperlyConfigured("settings.DATABASES is improperly configured. " django.core.exceptions.ImproperlyConfigured:
> settings.DATABASES is improperly configured. Please supply the ENGINE
> value. Check settings documentation for more details.

This is an old question but I am adding an answer for future visitors. You seem to have several errors:
If you already installed psycopg2, you should change the ENGINE name to django.db.backends.postgresql_psycopg2.
In your settings you specified your database NAME to be django, but in your pgAdmin screenshot it is seen that you don't have such a database. Create a database named django using pgAdmin.
The correct setup would be then:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

Related

TypeError: connect() argument 4 must be str, not WindowsPath

When I run this python manage.py migrate
return Connection(*args, **kwargs)
File "C:\Users\WyndhamKeitA\AppData\Roaming\Python\Python310\site-packages\MySQLdb\connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
**TypeError: connect() argument 4 must be str, not WindowsPath**
my DB under settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': BASE_DIR / 'db.crudpy',
'USERNAME': 'root',
'PASSWORD': '1234',
'PORT': '3306',
'HOST': 'localhost'
}
}
How do I get rid of the error?
crudpy is the schema I've created on mysql

django.db.utils.OperationalError: unable to open database file f

I have this kind of error
File"C:\Users\ComputerPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\
LocalCache\local-packages\Python39\site-packages\django\db\backends\sqlite3\base.py", line
205, in get_new_connection
conn = Database.connect(**conn_params)
django.db.utils.OperationalError: unable to open database file
while this is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'myapp',
'USER' : 'postgres',
'PASSWORD': 'er1234',
'HOST': 'localhost'
}
}

Peewee FlaskDB not accepting keyword arguments (eg. max_connections, stale_timeout)

The app I've been working on, which uses Python 3.9, Flask 2.0.2, peewee 3.14.4, and MySQL 8.0 works fine, except when I add configuration options for the FlaskDB dictionary of configuration options. I need to add MySQL configuration options (including SSL) when I deploy the app.
The FlaskDB configuration options which do work
DATABASE = {
'name': DB_NAME,
'engine': 'peewee.MySQLDatabase',
'user': DB_USERNAME,
'passwd': DB_PASSWORD,
'host': DB_HOST,
'port': DB_PORT}
The FlaskDB configuration options which do NOT work
DATABASE = {
'name': DB_NAME,
'engine': 'peewee.MySQLDatabase',
'user': DB_USERNAME,
'passwd': DB_PASSWORD,
'host': DB_HOST,
'port': DB_PORT,
'max_connections': 32,
'stale_timeout': 300}
The error I receive from the uwsgi log
Traceback (most recent call last):
File "/app/./wsgi.py", line 4, in <module>
from run import app
File "/app/./run.py", line 6, in <module>
app = create_app()
File "/app/./ticketsapi/__init__.py", line 33, in create_app
from ticketsapi.users.routes import users
File "/app/./ticketsapi/users/routes.py", line 8, in <module>
from ticketsapi.models import User, Post
File "/app/./ticketsapi/models.py", line 302, in <module>
initalisedB()
File "/app/./ticketsapi/models.py", line 233, in initalisedB
if User.table_exists():
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 6658, in table_exists
return cls._schema.database.table_exists(M.table.__name__, M.schema)
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3310, in table_exists
return table_name in self.get_tables(schema=schema)
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 4011, in get_tables
return [table for table, in self.execute_sql(query, ('VIEW',))]
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3142, in execute_sql
cursor = self.cursor(commit)
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3126, in cursor
self.connect()
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3080, in connect
self._state.set_connection(self._connect())
File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3982, in _connect
conn = mysql.connect(db=self.database, **self.connect_params)
TypeError: __init__() got an unexpected keyword argument 'max_connections'
Looking at page 287 of the peewee Documentation, Release 3.14.4 my syntax looks to be valid. From the peewee documentation:
DATABASE = {
'name': 'my_app_db',
'engine': 'playhouse.pool.PooledPostgresqlDatabase',
'user': 'postgres',
'max_connections': 32,
'stale_timeout': 600,
}
Any thoughts on where I'm going wrong?
The obvious answer, I needed to add playhouse.pool.PooledMySQLDatabase to the configuration options:
DATABASE = {
'name': DB_NAME,
'engine': 'playhouse.pool.PooledMySQLDatabase',
'user': DB_USERNAME,
'passwd': DB_PASSWORD,
'host': DB_HOST,
'port': DB_PORT,
'max_connections': 32,
'stale_timeout': 300}

Mongo + Postgres + Django Testing Error - Multiple Databases (Django-MongoDB-Engine)

I have been trying to get my django testing working for an API I am building. I am newer to Python and have been stuck on this for a little while.
Some quick facts:
I am using 2 databases - A postgresql database for my users and a mongo database for everything else.
I am using TastyPie, Django-MongoDB-Engine, Djangotoolbox, Django-Non-rel, Pymongo and psycopg2
I have successfully connected to the databases. When I save an auth_user from the shell it saves to the sql database and the models from my app save to the mongoDB
I have to integrate this with another API so was limited to what my options were for databases and libraries.
My big problem is with testing - I can't get it to work.
When I try to run Django's tests with ./manage.py test, I keep getting this error:
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
After extensive search, I am still not sure what it is. I know that obviously something is trying to authenticate with my MongoDB and then its not allowing it. I dont mind using this DB to test as it is a testing DB anyways.
Please help! My configuration and error messages are below.
I set up the database settings to include both databases, as so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'main_db',
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
},
'mongo': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'mongo_db',
'USER': os.environ['MONGO_USERNAME'],
'PASSWORD': os.environ['MONGO_PASSWORD'],
'HOST': os.environ['MONGO_HOSTNAME'],
'PORT': os.environ['MONGO_PORT'],
}
}
DATABASE_ROUTERS = ['myproject.database_router.AuthRouter']
As you can see, I created my own custom router:
sql_databases = ['admin', 'auth', 'contenttypes', 'tastypie'
'sessions', 'messages', 'staticfiles']
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations only if model does includes auth app.
"""
if obj1._meta.app_label in sql_databases or \
obj2._meta.app_label in sql_databases:
return True
return False
def allow_migrate(self, db, model):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if db == 'default':
return model._meta.app_label in sql_databases
elif model._meta.app_label in sql_databases:
return False
return False
The full trace is here:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 196, in create_test_db
self.connection._reconnect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 279, in _reconnect
self._connect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 268, in _connect
if not self.database.authenticate(user, password):
File "myproject/djenv/lib/python2.7/site-packages/pymongo/database.py", line 891, in authenticate
self.connection._cache_credentials(self.name, credentials)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 459, in _cache_credentials
auth.authenticate(credentials, sock_info, self.__simple_command)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 243, in authenticate
auth_func(credentials[1:], sock_info, cmd_func)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 222, in _authenticate_mongo_cr
cmd_func(sock_info, source, query)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 690, in __simple_command
helpers._check_command_response(response, None, msg)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/helpers.py", line 178, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
Any help is appreciated!
Wanted to give a update on how I fixed this - I thought it was a read/write issue (with the auth fails issue), but it was not.
If you see this error, its an authentication issue - Either your username, password, database name or collection name is incorrect.
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce',
u'blahblah'), ('key', u'blahblah')]) failed: auth fails
Django when setting up a test database adds 'test_' to the beginning of the database name. So my main database, called maindb is then created as 'test_maindb'.
You can see info regarding this at: Test Databases and Django
Ensure that you can write to a separate database that is named as your database name with 'test_' appended to the name.
Also - an alternative solution is to define test database setting in your database settings. This can be done by appending 'TEST_' to any of the database attributes. An example inlcudes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'maindb',
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
'TEST_NAME': 'my_test_sql',
'TEST_USER': 'test_sql_user',
'TEST_PASSWORD': 'password'
},
'mongo': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'mongodb',
'USER': os.environ['MONGO_USERNAME'],
'PASSWORD': os.environ['MONGO_PASSWORD'],
'HOST': os.environ['MONGO_HOSTNAME'],
'PORT': os.environ['MONGO_PORT'],
'TEST_NAME': 'my_test_mongodb',
'TEST_USER': 'test_mongo_user',
'TEST_PASSWORD': 'password'
}
}
Hope it helps! And please give feedback if you see anything additional

Can't query SQL Server from django using django-pyodbc

I'm trying to sync an SQL Server 2008 R2 database running remotely on IIS 7, to a django 1.6 app running python 3.3 on Windows 7, using manage.py syncdb. However I am being met with the error,
TypeError: The first argument to execute must be a string or unicode query.
I have django-pyodbc 0.2.3 and pyodbc 3.0.7 installed, with my settings.py DATABASES as,
{
'default': {
'ENGINE': 'django_pyodbc',
'HOST': '...',
'NAME': '...',
'OPTIONS': {
'host_is_server': True
}
}
}
As you may guess, USER and PASSWORD are omitted since I need Integrated_Security=Yes and Trusted_Connection=Yes for the connection. OPTIONS appears to have to be non-empty due to the way django-pyodbc initialises the class DatabaseWrapper, even though host_is_server is irrelevant on Windows.
The full error I'm receiving is:
Traceback (most recent call last):
File "Z:\python\ns_reports_server\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\commands\syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\__init__.py", line 157, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\base.py", line 290, in _cursor
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\operations.py", line 31, in _get_sql_server_ver
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\util.py", line 51, in execute
return self.cursor.execute(sql)
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\base.py", line 410, in execute
TypeError: The first argument to execute must be a string or unicode query.
If you look at the source code for the tenth call from the top, django_pyodbc/operations.py is querying the connection for the SQL Server version,
cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")
Yet, by the end of the call stack, this sql to be executed has disappeared. django_pyodbc/base.py has,
return self.cursor.execute(sql, params)
for which the first argument is not being recognised as a 'string or unicode query'.
Python, django, and SQL Server are all new for me so the answer might be obvious, but I can't for the life of me work it out. Cheers.
Edit: driver_supports_utf8=True as mention in other answers would be the correct fix.
It looks like this is problem with django-pyodbc and Python 3.
In base.py, line 367 is
sql = sql.encode('utf-8')
This line turns a string into bytes which is what is causing the TypeError. As you are
on Windows, I am assuming the driver can handle unicode. I suggest you comment out lines 364 to 367 (the first 4 in the format_sql function). This way your unicode strings will stay unicode and you won't get the TypeError.
https://github.com/lionheart/django-pyodbc/blob/master/django_pyodbc/base.py
def format_sql(self, sql, n_params=None):
if not self.driver_supports_utf8 and isinstance(sql, text_type):
# Older FreeTDS (and other ODBC drivers?) don't support Unicode yet, so
# we need to encode the SQL clause itself in utf-8
sql = sql.encode('utf-8')
# pyodbc uses '?' instead of '%s' as parameter placeholder.
if n_params is not None:
try:
sql = sql % tuple('?' * n_params)
except:
#Todo checkout whats happening here
pass
else:
if '%s' in sql:
sql = sql.replace('%s', '?')
return sql
I have raised an issue with django-pyodbc which goes into a little more detail.
https://github.com/lionheart/django-pyodbc/issues/47
You can also fix this in your configuration options. I fought with this for a while. Try changing your DB to be config'd like this for 1.6:
DATABASES = {
'default': {
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'your_password',
'HOST': 'database.domain.com,1433',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0'
},
}
}
If you're on Windows, the "extra_params" gets ignored, but it makes it portable to Linux.
This issue solved for us by adding 'driver_supports_utf8': True,'
'characteristics': {
'ENGINE': "django_pyodbc",
'HOST': "ourdbhost.com",
'PORT': 5555,
'NAME': "OurDB",
'USER': "ouruser",
'PASSWORD': "acoolpw",
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True,
'extra_params': 'TDS_Version=7.1',
},
},
worked as suggested by gri on https://github.com/lionheart/django-pyodbc/issues/47
adding
'autocommit': True,
'unicode_results': True,
to OPTIONS did not fix it, but did not break anything either

Categories