3

I want to add full text search to my Django project and I used PostgreSQL and docker,so want to add extension pg_trgm to PostgreSQL for trigram similarity search. how should I install this extension with dockerfile?

In shared my repository link.

FROM python:3.8.10-alpine
WORKDIR /Blog/


ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1


RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev


RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt


COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

COPY . .

ENTRYPOINT ["./entrypoint.sh"]

docker-compose

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/Blog
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    depends_on:
      - db

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=helo
      - POSTGRES_PASSWORD=helo
      - POSTGRES_DB=helo
    
volumes:`enter code here`
  postgres_data:
1

2 Answers 2

2

You can do this the hard way!

$ sudo docker-compose exec db bash
$ psql -U username -d database
$ create extension pg_trgm;

This is not a good method, because you have to be careful and reinstall it every time the image is created.

or

use default django solution:

https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/operations/#trigramextension

from django.contrib.postgres.operations import TrigramExtension

class Migration(migrations.Migration):
    ...

    operations = [
        TrigramExtension(),
        ...
    ]
Sign up to request clarification or add additional context in comments.

1 Comment

I've been struggling trying to install this through docker-compose for ages, doing all sorts of weird stuff manipulating databases on startup, but this was all I needed!! thank you!! This applies to everything in docs.djangoproject.com/en/4.1/ref/contrib/postgres/operations
2

For someone who is building a PostgreSQL image from scratch another cleaner way would be to include all extensions that he/she wants to build the image with by specifying them under environments variable using , POSTGRES_EXTENSIONS: (inside docker-compose.yml file)

    services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/Blog
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    depends_on:
      - db

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      POSTGRES_USER: helo
      POSTGRES_PASSWORD: helo
      POSTGRES_DB: helo
      POSTGRES_EXTENSIONS: pg_trgm ,your_other_extension
    
volumes:`enter code here`
  postgres_data:

Replace your_other_extension with the name of the extension you want to install, and make sure to separate them with a comma.

In addition to POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_USER, and POSTGRES_EXTENSIONS, you can set other environment variables for the PostgreSQL container as well. Here are some commonly used ones:

  • POSTGRES_HOST_AUTH_METHOD -The authentication method used for connecting to the database
  • PGDATA: - The location of the PostgreSQL data directory
  • POSTGRES_INITDB_ARGS: Additional arguments to pass to the initdb command when creating the database cluster
  • Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.