cannot create extension without superuser role

前端 未结 6 1437
孤街浪徒
孤街浪徒 2020-12-23 11:12

I\'m trying to run unit tests in Django, and it creates a new database. The database has postgis extensions and when I regularly create the database, I use \"CREATE ExTENSIO

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 12:00

    You can also install postgis to the template1 database template which is inherited by default by all newly created database.

    $ psql -U postgres -d template1 -c "CREATE EXTENSION postgis;"
    

    All new databases created from this point will have the postgis extension installed, including Django's test database, unless they specify a different template when creating a database.

    If having postgis installed to all newly created databases is not desirable, you can create a new template, install postgis in it, and then have Django use this template when creating the test database.

    $ createdb template_postgis;  # create a new database
    $ psql -U postgres -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';"  # make it a template
    $ psql -U postgres -d template_postgis -c "CREATE EXTENSION postgis;"  # install postgis in it
    

    Then in Django settings:

    ...
    DATABASES = {
        'default': {
            ...
            'TEST': {
                'TEMPLATE': 'template_postgis',
            },
        },
    }
    

提交回复
热议问题