【问题标题】:PostgreSQL: Create extension not working when called from init scriptPostgreSQL:从初始化脚本调用时创建扩展不起作用
【发布时间】:2020-03-19 20:02:01
【问题描述】:

我正在使用 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.

我能做什么?

感谢您的帮助。

【问题讨论】:

标签: django postgresql docker


【解决方案1】:

你可以创建一个custom migration for creating an extension. 这样它就不受 docker 或环境的限制了。

from django.contrib.postgres.operations import CreateExtension
class Migration(migrations.Migration):
    ...

    operations = [
        CreateExtension(name='pg_trgm'),
        ...
    ]

【讨论】:

  • 那行得通。我仍然不知道为什么它不起作用,但我不再需要了。谢谢!
  • @schillingt 请问我打算在哪个文件中包含您发布的代码?
  • @toluwalemi 您实际上需要像数据迁移一样创建它:docs.djangoproject.com/en/3.1/topics/migrations/…
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多