Reputation: 48047
I am am working on setting up Django Project for running tests
. But I am getting below error:
Got an error creating the test database: permission denied to copy database "template_postgis"
Note: My default application's database is working fine. The issue is happening while running tests
.
Complete stack trace is as:
moin@moin-pc:~/workspace/mozio$ python manage.py test --verbosity=3
nosetests --verbosity=3
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
Creating test database for alias 'default' ('test_my_db')...
Got an error creating the test database: permission denied to copy database "template_postgis"
Type 'yes' if you would like to try deleting the test database 'test_mozio_db_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: must be owner of database test_mozio_db_test
Below is the DATABASE configuration of setting.py
:
POSTGIS_VERSION = (2, 2, 2)
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_pass',
'HOST': '<HOST_IP',
'PORT': '',
'TEST': {
'NAME': 'test_my_db',
},
}
}
Any help regarding this? Below are the steps I tried:
Grant create DB access to user:
ALTER USER my_user CREATEDB;
Grant all privileges to user for test_my_db
database:
GRANT ALL PRIVILEGES ON DATABASE test_mozio_db_test to mozio;
Edit: After fixing above issue, I was also getting error as:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
Updated my answer to fix both the issues.
Upvotes: 5
Views: 7659
Reputation: 48047
I finally found how to fix this issue. The problem is that when I created template_postgis
, I didn’t set it to be a template
.
You can check it via doing:
SELECT * FROM pg_database;
You can fix it by setting datistemplate=true
for template_postgis
by running:
update pg_database set datistemplate=true where datname='template_postgis';
After that in case you are getting error related to geometry
like:
django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5: "polygon" geometry(POLYGON,4326) NOT NULL,
That is because you need to add extension postgix
with database. In order to fix this, add postgis
to template_postgis
like:
psql -d psql -d template_postgis -c 'create extension postgis;'
Note: You must be superuser to create this extension.
Upvotes: 6
Reputation: 1155
Install packages first correctly.
sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
During installation postgres user is created automatically.
sudo su - postgres
You should now be in a shell session for the postgres user. Log into a Postgres session by typing:
psql
CREATE DATABASE myproject;
In your settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Upvotes: 0