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

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

Related

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}

Django: DATABASES IMPROPERLY CONFIGURED, Please supply engine value. (Multiple databases)

Hey guys I am trying to have 2 postgres databases in my django project, one to hold user data and the other to hold other contents, but I am getting this error when I try to run createsuperuser but I was able to run both makemigrations and migrate --database=users_db and the same for other db as well.
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.
From the django documentation, they have mentioned that it is okay to leave the default dict blank.
If the concept of a default database doesn’t make sense in the context of your project, you need to be careful to always specify the database that you want to use. Django requires that a default database entry be defined, but the parameters dictionary can be left blank if it will not be used. To do this, you must set up DATABASE_ROUTERS for all of your apps’ models, including those in any contrib and third-party apps you’re using, so that no queries are routed to the default database.
This is settings.py
DATABASES = {
'default': {},
'users_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'users_db',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST':'127.0.0.1',
'PORT':'5432',
},
'content_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'content_II',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST':'127.0.0.1',
'PORT':'5432',
},
}
DATABASE_ROUTERS = ['personal.routers.db_routers.AuthRouter', 'personal.routers.db_routers.PersonalDBRouter', ]
This is the db_routers.py
class AuthRouter:
route_app_labels = {'sessions', 'auth', 'contenttypes', 'admin', 'accounts'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return 'users_db'
return None
class PersonalDBRouter:
route_app_labels = {'actions', 'blog', 'token_blacklist', 'taggit', 'django_celery_beat', 'django_celery_results',}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'content_db'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'content_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return 'content_db'
return None
This is Account Manager and when I try to run createsuperuser, it shows an error in the line where create_user saves.
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, first_name, last_name, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')
if not first_name:
raise ValueError('Users must have a First Name') # check later
user = self.model(
email=self.normalize_email(email),
username=username,
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save(using="users_db")
return user
def create_superuser(self, email, username, first_name, last_name, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
first_name=first_name,
last_name=last_name,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
I don't understand what's causing this error as I have already provided the engine value and cannot find the error. Can someone tell me where the issue is?
Thanks
UPDATE
TRACEBACK:
(myvenv) C:\Demo_1\mainsite>python manage.py createsuperuser --
database=users_db
Email: admin#test.com
Username: admin
First name: Admin
Last name: Test
Password:
Password (again):
Traceback (most recent call last):
File "C:\Demo_1\mainsite\manage.py", line 21, in <module>
main()
File "C:\Demo_1\mainsite\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "C:\Demo_1\mainsite\accounts\models.py", line 98, in create_superuser
user = self.create_user(
File "C:\Demo_1\mainsite\accounts\models.py", line 94, in create_user
user.save()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1414, in execute_sql
with self.connection.cursor() as cursor:
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\danny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\dummy\base.py", line 20, 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.
Try to remove the using in the save:
def create_user(...):
...
user.save()
def create_superuser(...):
...
user.save()
EDIT:
Just noticed your routers don't have db_for_write (you wrote db_for_read twice):
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users_db'
return None
Set your DataBase like :-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'users_db',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST': 'localhost',
'PORT': '5432',
}
'content_db': {
'NAME': 'django.db.backends.postgresql_psycopg2',
'NAME': 'content_II',
'USER': 'postgres',
'PASSWORD': 'Trial_user123',
'HOST': 'localhost',
'PORT': '5432',
}
}
You have to migrate both databases differently like :-
$ ./manage.py migrate --database=users_db
$ ./manage.py migrate --database=content_II
Create a superuser in different database like :-
./manage.py createsuperuser --database=users_db

Django databases: problems with the test database

I am learning about Django testing.
I wrote a simple App with a simple model and would like to run tests to check the validity of a model method, but I get an error message when I run the test:
here's models.py
from django.db import models
class Trip(models.Model):
origin = models.CharField(max_length=20)
destination = models.CharField(max_length=20)
def __str__(self):
return self.origin
def is_valid(self):
return self.origin != self.destination
Here's test.py
from django.test import TestCase
from .models import Trip
# Create your tests here.
class TripModelTests(TestCase):
def test_trip(self):
a = Trip.objects.create(origin='a', destination='a')
self.assertIs(a.is_valid(), True)
here is settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd57r9kcrhthdc7',
'USER': 'sdqxaruartlvrd',
'PASSWORD': 'e7b8f85611596ed125fe3ed4ea590f821f65e317c17ee7871be75b8130d72378',
'HOST': 'ec2-3-214-46-194.compute-1.amazonaws.com',
'PORT': '5432',
'TEST': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}
and here is the error message i get when I run python manage.py test transport
Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 614, in setup_databases
return _setup_databases(
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\utils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 55, in create_test_db
self._create_test_db(verbosity, autoclobber, keepdb)
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 172, in _create_test_db
'dbname': self.connection.ops.quote_name(test_database_name),
File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\operations.py", line 113, in quote_name
if name.startswith('"') and name.endswith('"'):
AttributeError: 'WindowsPath' object has no attribute 'startswith'
The test works fine if I just use the default django settings and use a sqlite database....
The error could be due to your BASE_DIR path, in your settings.py, you need to remove the slash / and switch it over to the following
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
You may need to verify depending on your BASE_DIR contents that it points to the right place. One way to debugging this is to set ipdb() just after your database dictonary, so once you use python manage.py runserver, can can easily inspect the DATABASES structure.
import ipdb; ipdb.set_trace()
Source: https://pypi.org/project/ipdb/

django.core.exceptions.ImproperlyConfigured: The database name '****.amazonaws.com' (85 characters) is longer than PostgreSQL's limit of 63 characters

I am trying to migrate my Django model to the AWS Lightsail database, but I am getting below error. My database is on AWS Lightsail. it is PostgreSQL. I am deploying my project on AWS so I have set up almost everything except model migration.
AMAZON DATABASE AVAILABLE CONNECTION OPTIONS:
Endpoint-> ************.ap-******-1.***.amazonaws.com,
Port-> 5444,
User name-> db,
Password-> Something
MY SETTINGS.PY FILE DATABASE CONNECTION OPTIONS:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '****Endpoint*****',
'USER': 'db',
'PASSWORD': 'Something',
'HOST': '5444'
}
}
Terminal Error:
> `2, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", lin
e 76, in applied_migrations
if self.has_table():
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/migrations/recorder.py", lin
e 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
260, in cursor
return self._cursor()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
236, in _cursor
self.ensure_connection()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
220, in ensure_connection
self.connect()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line
196, in connect
conn_params = self.get_connection_params()
File "/home/bitnami/portfolio/ManojJha-portfolio/.venv/lib/python3.7/site-packages/django/db/backends/postgresql/base.py"
, line 165, in get_connection_params
self.ops.max_name_length(),
django.core.exceptions.ImproperlyConfigured: The database name '*****************Database End-point*****************.***.amazonaws.com' (85 characters) is longer than PostgreSQL's limit of 63 characters. Supply a shorter NAME in settings.DATABASES.```
So is there any way to shorten the endpoint name or there is any other solution to handle this error. Please guide me to set up this.
Thanks,
-Manoj Jha
"HOST" should be the hostname, not the port!
"NAME" is the name of the database.
An example from the Django documentation:
'ENGINE': 'django.db.backends.oracle',
'NAME': 'xe',
'USER': 'a_user',
'PASSWORD': 'a_password',
'HOST': 'dbprod01ned.mycompany.com',
'PORT': '1540',

403 Missing or insufficient permissions

I am running datastore emulator. When I run "dev_appserver.py app.yaml" command, I get this error
403 Missing or insufficient permissions.
This is the warning I get. I know there is a problem with authentication. I have gone through this. But couldn't end up finding a solution.
Some details:
I am using MySQL databases for some apps.
For others I want to use datastore.
I am running datastore emulator on one tab, Google cloud proxy on another, dev_appserver on the third one.
I have set the environment variables using the command gcloud beta emulators datastore env-init.
My settings.py
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/connectionname',
'NAME': 'db_name',
'USER': 'username',
'PASSWORD': 'password',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3307',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
}
}
# [END db_setup]
My app.yaml
runtime: python37
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
Technology background:
Django 2.1
Python 3.5.2
Stack trace:
Traceback (most recent call last):
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/salman/chaipani_env/project_chaipani/chaipani/views.py", line 20, in post_new
post = models.insert(data)
File "/home/salman/chaipani_env/project_chaipani/chaipani/models.py", line 15, in insert
client.put(entity)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 404, in put
self.put_multi(entities=[entity])
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 431, in put_multi
current.commit()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/batch.py", line 273, in commit
self._commit()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore/batch.py", line 249, in _commit
self.project, mode, self._mutations, transaction=self._id)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/cloud/datastore_v1/gapic/datastore_client.py", line 426, in commit
request, retry=retry, timeout=timeout, metadata=metadata)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/gapic_v1/method.py", line 139, in __call__
return wrapped_func(*args, **kwargs)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/retry.py", line 260, in retry_wrapped_func
on_error=on_error,
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/retry.py", line 177, in retry_target
return target()
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/timeout.py", line 206, in func_with_timeout
return func(*args, **kwargs)
File "/tmp/tmpIZrOSY/lib/python3.5/site-packages/google/api_core/grpc_helpers.py", line 61, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions.
INFO 2018-10-10 10:51:26,298 module.py:880] default: "POST /users/post/new/ HTTP/1.1" 500 133823
It seems that your Default service account does not have enough permissions. It might require some additional permissions such as "Datastore Index Admin" to infer over a Datastore[1]. I am not sure on how this will work in the Datastore emulator, but since it aims to provide a local emulation, maybe it will get the roles and permission to perform a test. Could you please check the permissions in your service account[2], change if needed and let me know if the issue persists. I will be waiting for your reply.
[1]https://cloud.google.com/appengine/docs/flexible/nodejs/granting-project-access#before_you_begin
[2]https://cloud.google.com/iam/docs/granting-roles-to-service-accounts

Categories