6

I am using postgres with my django app and I had manually created the hstore extension in the database. But, when I run tests it tries to create a new database and fails when the hstore extension is not found.

I am getting the following errors:

django.db.utils.ProgrammingError: hstore type not found in the database. please install it from your 'contrib/hstore.sql' file

I have update my migrations as per this post and it's as follows:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
from django.contrib.postgres.operations import HStoreExtension
import django.contrib.postgres.fields
import django.contrib.postgres.fields.hstore


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        HStoreExtension(),
        migrations.CreateModel(
        name='AppUser',
        fields=[
            ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ('created_date', models.DateTimeField(auto_now_add=True)),
            ('modified_date', models.DateTimeField(auto_now=True)),
            ('lock', models.BooleanField(default=False)),
            ('username', models.CharField(max_length=100)),
            ('email', models.EmailField(max_length=150)),
            ('social_data', django.contrib.postgres.fields.hstore.HStoreField(blank=True, default='')),
            ('phone_number', models.CharField(max_length=15)),
            ('photo', models.URLField(blank=True)),
            ('gender', models.TextField(max_length=6)),
            ('badges', django.contrib.postgres.fields.ArrayField(size=None, base_field=models.CharField(max_length=30))),
        ],
        options={
            'abstract': False,
        },
    ),
    ]
Community
  • 1
  • 1
Sourabh Dev
  • 743
  • 11
  • 22
  • that's because migrations are not run when executing tests – yedpodtrzitko Mar 31 '16 at 03:08
  • Then, how do I force the test db to create a hstore extension? – Sourabh Dev Mar 31 '16 at 08:19
  • http://stackoverflow.com/questions/11584749/how-to-create-a-new-database-with-the-hstore-extension-already-installed – yedpodtrzitko Mar 31 '16 at 08:38
  • I have already taken the above steps on creating hstore extension. It's the test where the problem arises as a new test db is being created with the hstore extension. – Sourabh Dev Apr 01 '16 at 03:16
  • I dont understand. Your problem is, quote: "new test db is being created with the hstore extension"? Isnt it the result you wanted to achieve? – yedpodtrzitko Apr 01 '16 at 04:08
  • Oh! Sorry, I had a typo there... I meant isn't. – Sourabh Dev Apr 01 '16 at 05:57
  • Try harder. Does the testing database exist? if so, drop it now. Did you modifed the default template to contain the `hstore` extension? If not, do it now. Now create a database manually `createdb something`, and check the extensions in the (`\dx`). Is hstore there? Good. It will be in the testing database too. – yedpodtrzitko Apr 01 '16 at 06:21

2 Answers2

7

As it stated in documentation:

The hstore extension is not automatically installed on use with this package: you must install it manually.

For this you must connect to the database and create an extension:

CREATE EXTENSION hstore;

To run tests, hstore extension must be installed on template1 database. To install hstore on template1:

$ psql -d template1 -c 'create extension hstore;'

For me it worked like a charm! :)

Check if extension is working: connect to required database with correspondent user, and issue:

`CREATE table test(id serial, value hstore);'

You should get: CREATE TABLE response if everything is ok.

lospejos
  • 1,976
  • 3
  • 19
  • 35
M.Void
  • 2,764
  • 2
  • 29
  • 44
1

The following steps will help you to create hstore extension.

$ sudo su - postgres

$ psql

$ \c data_base_name

$ CREATE EXTENSION hstore;

It's worked for me.

Community
  • 1
  • 1
Muneer Muhammed
  • 883
  • 3
  • 10
  • 29