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.
Related
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.
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
Hi have a django project a full project now I want to migrate to mysql from the default Sqlite3 which is the default database. I am on a Mac OS and I don't know how to achieve this process. Any one with a complete guide on how to make the switch would be appreciated.
Go to your project's settings file and edit DATABASES with proper database connection
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
Now open mysql and create database as you give in DATABASE settings.
Go to your project -
./manage makemigrations
./manage migrate
This will create all the tables in the specified database name.
I am an extreme newbie to Django and using shell. Hence please be gentle. I am working on a site where the owner has lost the relationship with the developer and hence passwords to the admin accounts (front-end and back-end). I am trying to create superusers for both but am having problems with the database. The site uses a PostgreSQL database. In the shell I activate the virtual environment and run my command:
python3 manage.py createsuperuser
The email address is requested and enter but receive the error after several lines of script.
django.db.utils.OperationalError: FATAL: password authentication failed for the user "xxx".
Do I need to somehow activate or enable the connection to the database before running the command?? Again really new and not trying to be the developer on the site- just trying to gain access and create users. Many thanks.
ADDITION BASED ON CONVERSATION BELOW
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'hci',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
except from base.py
from .base import *
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = True
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
DATABASES['default'].update({
'NAME': 'xxx_hcidemo',
'USER': 'xxx_hcidemo',
'PASSWORD': 'xxxxxxxxxx',
'HOST': 'localhost',
})
BROKER_URL = 'redis://localhost:11201/0'
CELERY_RESULT_BACKEND = 'redis://localhost:11201/0'
entire demo.py file with password and username removed....
Solution was found! Many thx- the manage.py file was pointing at demo database - had to change to point at the production database. Completely agree - bad set-up but I now have access.
Probably You (or someone else) change password to database.
Look at Your DATABASES settings in Your settings.py file and update PASSWORD field.
EDIT:
Your manage.py use different database settings, than site. Remove if __name__ == '__main__': from manage.py file and try to add user.
BTW. It's very bad solution to differentiation prod env from dev env in this way.
I'm new to Django and MySQL, and am trying to continue a project written by a previous developer.
I've just finished a fresh install of MySQL (didn't set any password). When I run python manage.py syncdb, I get the error:
python manage.py syncdb
/home/home/.virtualenvs/sorrento/local/lib/python2.7/site-packages/debug_toolbar/settings.py:134: DeprecationWarning: INTERCEPT_REDIRECTS is deprecated. Please use the DISABLE_PANELS config in theDEBUG_TOOLBAR_CONFIG setting.
"DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)
OperationalError: (1045, "Access denied for user 'home'#'localhost' (using password: NO)")
This is the database config in settings.py:
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '',
}
}
########## END DATABASE CONFIGURATION
Since NAME is empty, does this mean there isn't a database set up? Do I need to get a database dump from the previous developer? Or is there something else I'm missing?
It is telling that you don't have permissions to edit the DB. To be able to connect you will need the NAME of the database and a USER and PASSWORD to connect with.
You don't need to get a copy of the db from the previous developer unless you actually need any of the data from there.
You have a fresh install of MySQL, so you just need to create an (empty) database.
Firstly, edit your db settings so that the USER attribute is 'root' and the NAME attribute is a relevant name (perhaps the name of your project).
Then, from the shell, do mysql -u root and then CREATE DATABASE <dbname> where dbname is the NAME you used above.
Now you should be able to run syncdb.