20

When I try to install the unaccent Postgres extension (through the postgresql-contrib package), everything works as per the below:

# psql -U postgres -W -h localhost
Password for user postgres:
psql (9.3.9)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.

postgres=# CREATE EXTENSION unaccent;
CREATE EXTENSION
postgres=# SELECT unaccent('Hélène');
 unaccent
----------
 Helene
(1 row)

However, when I try to use with Django 1.8, I get following error:

ProgrammingError: function unaccent(character varying) does not exist
LINE 1: ...able" WHERE ("my_table"."live" = true AND UNACCENT("...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Using Postgresql 9.3 and Django 1.8.

SaeX
  • 17,240
  • 16
  • 77
  • 97

1 Answers1

44

A migration file needs to be manually made and applied.

First, create an empty migration:

./manage.py makemigrations myapp --empty

Then open the file and add UnaccentExtension to operations:

from django.contrib.postgres.operations import UnaccentExtension


class Migration(migrations.Migration):

    dependencies = [
        (<snip>)
    ]

    operations = [
        UnaccentExtension()
    ]

Now apply the migration using ./manage.py migrate.

If you'd get following error during that last step:

django.db.utils.ProgrammingError: permission denied to create extension "unaccent"
HINT:  Must be superuser to create this extension.

... then temporarily allow superuser rights to your user by performing postgres# ALTER ROLE <user_name> SUPERUSER; and its NOSUPERUSER counterpart. pgAdminIII can do this, too.

Now enjoy the unaccent functionality using Django:

>>> Person.objects.filter(first_name__unaccent=u"Helène")
[<Person: Michels Hélène>]
SaeX
  • 17,240
  • 16
  • 77
  • 97