我正在使用docker-compose创建一个能够使用pg_trgm
扩展名执行三字组相似度搜索的数据库:
postgres-db:
restart: always
image: postgres:12.2
env_file:
- ../../.envs/_compose_prod.env
expose:
- 5432
volumes:
- database-data:/var/lib/postgresql/data/
- ../entrypoints/init.sql:/docker-entrypoint-initdb.d/init.sql
../ entrypoints / init.sql:
create extension pg_trgm;
我首先使用了docker-compose down -v
。这是docker-compose up --build
输出的摘录:
postgres-db_1 | 2020-03-19 19:36:42.352 UTC [46] LOG: database system was shut down at 2020-03-19 19:36:42 UTC
postgres-db_1 | 2020-03-19 19:36:42.360 UTC [45] LOG: database system is ready to accept connections
postgres-db_1 | done
postgres-db_1 | server started
postgres-db_1 | CREATE DATABASE
postgres-db_1 |
postgres-db_1 |
postgres-db_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
postgres-db_1 | CREATE EXTENSION
postgres-db_1 |
postgres-db_1 |
postgres-db_1 | 2020-03-19 19:36:42.643 UTC [45] LOG: received fast shutdown request
postgres-db_1 | waiting for server to shut down....2020-03-19 19:36:42.647 UTC [45] LOG: aborting any active transactions
postgres-db_1 | 2020-03-19 19:36:42.649 UTC [45] LOG: background worker "logical replication launcher" (PID 52) exited with exit code 1
postgres-db_1 | 2020-03-19 19:36:42.650 UTC [47] LOG: shutting down
postgres-db_1 | 2020-03-19 19:36:42.686 UTC [45] LOG: database system is shut down
postgres-db_1 | done
postgres-db_1 | server stopped
postgres-db_1 |
postgres-db_1 | PostgreSQL init process complete; ready for start up.
postgres-db_1 |
postgres-db_1 | 2020-03-19 19:36:42.757 UTC [1] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
似乎扩展程序创建成功。我已经在自己的计算机上成功完成了此操作,因此我希望它现在可以正常工作,但是当使用queryset.annotate(similarity=TrigramSimilarity("title", value_to_search_for))
并评估Django REST Framework应用程序中的查询集时,我得到以下信息:
postgres-db_1 | 2020-03-19 19:36:55.384 UTC [81] ERROR: function similarity(text, unknown) does not exist at character 146
postgres-db_1 | 2020-03-19 19:36:55.384 UTC [81] HINT: No function matches the given name and argument types. You might need to add explicit type casts.
我该怎么办?
感谢您的帮助。
答案 0 :(得分:1)
您可以创建一个custom migration for creating an extension.,而不仅限于docker或环境。
from django.contrib.postgres.operations import CreateExtension
class Migration(migrations.Migration):
...
operations = [
CreateExtension(name='pg_trgm'),
...
]