Django Test: error creating the test database: permission denied to copy database "template_postgis"
<p>I am am working on setting up Django Project for <code>running tests</code>. But I am getting below error:</p>

<pre><code>Got an error creating the test database: permission denied to copy database "template_postgis"
</code></pre>

<p><strong>Note</strong>: My default application's database is working fine. The <code>issue is happening while running tests</code>.</p>

<p>Complete stack trace is as:</p>

<pre><code>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
</code></pre>

<p>Below is the DATABASE configuration of <code>setting.py</code>:</p>

<pre><code>POSTGIS_VERSION = (2, 2, 2)

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'my_db',
        'USER': 'my_user',
        'PASSWORD': 'my_pass',
        'HOST': '&lt;HOST_IP',
        'PORT': '',
        'TEST': {
            'NAME': 'test_my_db',
        },
    }
}
</code></pre>

<p>Any help regarding this? Below are the steps I tried:</p>

<ul>
<li><p>Grant create DB access to user:</p>

<pre><code>ALTER USER my_user CREATEDB;
</code></pre></li>
<li><p>Grant all privileges to user for <code>test_my_db</code> database:</p>

<pre><code>GRANT ALL PRIVILEGES ON DATABASE test_mozio_db_test to mozio;
</code></pre></li>
</ul>

<hr>

<p><strong>Edit</strong>:
After fixing above issue, I was also getting error as:</p>

<pre><code>django.db.utils.ProgrammingError: type "geometry" does not exist
LINE 5:     "polygon" geometry(POLYGON,4326) NOT NULL,
</code></pre>

<p>Updated my answer to fix both the issues.</p>

<p>Install packages first correctly.</p>

<pre><code>sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
</code></pre>

<p>During installation postgres user is created automatically. </p>

<pre><code> sudo su - postgres
</code></pre>

<p>You should now be in a shell session for the postgres user. Log into a Postgres session by typing:</p>

<pre><code> psql

 CREATE DATABASE myproject;
</code></pre>

<p>In your settings.py.</p>

<pre><code>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}
</code></pre>

python django postgis django-testing geodjango