How could I create a postgresql extension in django test setup

Viewed 1190

I'm using django 1.11 and PostgreSQL 9.6. When I want to test an app, I find that the django test will create and use a test database.This test database doesn't have an extension that need to be created first.

So how to create an extension after create the test database and before create the test data?

1 Answers

What you should do is apply your extensions to the template1 database which is used for creating the new database for tests. As it is stated in this article explaining the template0 and template1 databases:

Whatever you put into template1 will be available in a new database if you use the following syntax: “create database [DB_NAME];” This can simplify your deployments a lot if you rely on pre-installed objects for e.g. monitoring or development.

Simply run:

psql -d template1 -c 'command to create your extension here;'
Related