How to manage django'orm via aws redshift? - python

I'm a new developer for AWS redshift.
As far as, I knew AWS redshift is pretty similar to postgresql
Is there any library or module for manage(like auto-migration, auto migrate) between redshift and Django?
I did create a table and access data by using SQLAlchemy and redshift but I don't know how to do that thing in python Django.
any suggestion or idea is okay!!
I got this error with this command
python manage.py migrate
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (Column "django_migrations.id" has unsupported type "serial".

You could easily connect your RedShift services with your Django App.
Try to add the code below to your settings.py file
DATABASES = {
'default': {
'NAME': '[Your Cluster Name]',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': '[Username ]',
'PASSWORD': '[Password]',
'HOST': '[Pathname]',
'PORT': 5439,
},
}
But, you need to know that Redshift doesn't enforce primary keys. In my opinion is not a great idea to use Redshift as your default DB in your Django Application as Redshift is a helpful DB for a data warehouse and not for an app.
To sump up, my suggestion is PostgreSQL.

Related

Database config for Django testing

I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
I've checked user's permissions and I'm sure that this user have permission to create database.
My database config looks like this:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '****',
'PORT': '****',
}
}
My postgres db is on a server.
My questions are:
What is the right way to config my db and run tests?
Should I be using sqlite for testing?
If so how my code should look like, so I don't have to comment configs?
It looks like your DB user doesn't have permission to create a new database. Please, take a look here. This command-line utility allows you to create a user and set their permissions.
Example:
createuser my_user --createdb -W --username postgres
Note: you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.
Answering your questions:
You may have several configs for different stages, e.g development, testing, production.
You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.

How to connect django project to mysql database if the mysql database reside in wamp server?

I googgled alot and see many solutions, but none of them worked for me. I have a project in django and also an app as well. now, I want a connection to mysql database that reside in wamp server in order to interact (sending data or getting data) to database through my app.I also installed MYSQLDB for python. and I have done neccessary things in settings.py file.following is content of settings.py regradding of database
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'flask',
'USERNAME':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'',
}
}
In this link, you can find information about Django setup for WAMP. Also you may need to add port number to the database settings which is 3306 as default.

Django refuse to drop PostgreSQL database [another session using the database]

I try to setup postgresql for django, with the following setting:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'tangorblog_features',
'TEST': {
'NAME': 'tangorblog_features'
}
},
}
The idea is to test using development server with selenium and radish-bdd. I will run the development server, and let selenium and Django LiveServerTestCase to test against that server, without creating a separate database. So each time the test run, the database is reset. But Django refuse that there are other session that using the database.
However when I use mysql with the same settings like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tangorblog_features',
'HOST': 'localhost',
'PORT': '',
'USER': 'goat',
'PASSWORD': '',
'TEST': {
'NAME': 'tangorblog_features'
}
},
}
The test runs without a problem, about database being used in another session. I think that this is the PostgreSQL problem. How can I tweak it, so it could behave like MySQL?
this happens because mysql and postgres treat database differently. Mysql database is what postgres calls schema. DROP SCHEMA would also happen quetly, just like https://dev.mysql.com/doc/refman/5.7/en/drop-database.html DROP SCHEMA in mysql:
DROP SCHEMA is a synonym for DROP DATABASE.
Differently for postgres you connect to the database to work with schemas, so if smbd wants to drop database you have terminate their sessions. Not like in mysql
Dropping a database does not remove any TEMPORARY tables that were
created in that database. TEMPORARY tables are automatically removed
when the session that created them ends.
which means session is not aborted on database drop.
In order to force database drop and regarding the fact
...it cannot be executed while you or anyone else are connected to the
target database
you have to terminate accordiing backends first, you can do it with
select pg_terminate_backend(pid)
from pg_stat_activity
where datname = 'tangorblog_features';
Of course you have to be connected to some other db yourself

How to add Redis Database in Django-1.9?

I Want to add Redis Database in Django-1.9, so i followed this documents for integration
https://niwinz.github.io/django-redis/latest/
But i didn't find any clue on how to mention Database name in the settings, Here i want to mention Redis as a database on behalf Sqlite3, If uncommented this line django is throwing an Error of DATABASE not found
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'sqlite3'),
}
}
Thanks in Advance for your solution
What's django-redis?
django-redis is a BSD Licensed, full featured Redis cache/session
backend for Django.
What's redis
Redis is an open source (BSD licensed), in-memory data structure
store, used as a database, cache and message broker
Essentially that means django-redis is a django package that allows you to replace the default memcache as django's cache backend and also allows you to replace the DB as the default session storage. However django-redis does not implement features required to use it as a replacement for sqlite3 or any other database.
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
}
}
}
To use the redis database in django you need to add this code to your settings file,based on your requirement you can change the value of the database at the end of LOCATION value like ("redis://127.0.0.1:6379/1") for database '1'.
You can also check here: https://niwinz.github.io/django-redis/latest/#_configure_as_cache_backend
By default Django provides no support for non-relational database backends. However, if you intend to use Redis as your primary database, you can take a look at Django non-rel.

Testing Django app with Postgis Backend

I'm attempting to run tests on a GIS Django application running PostGIS as a database backend.
When I attempt to run tests, I get the following error:
django.db.utils.ProgrammingError: permission denied to create extension "postgis"
HINT: Must be superuser to create this extension.
The error makes sense. Only admin database users can install extensions since this privilege allows the execution of arbitrary external code. BUT since the test runner has to re-create the database each time the tests are run, Django's database user can't proceed.
Here is my database configuration.
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_crazy_secure_password',
'HOST': '127.0.0.1',
'PORT': '',
'TEST_NAME': 'test_my_db',
},
}
My solution to this was surprisingly simple, once I figured it out.
Connect to the template1 database, and run CREATE EXTENSION IF NOT EXISTS postgis;. The template1 database is copied when a new database is created, so all new databases will already have the extension installed.

Categories

Resources