Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose and Postgres Extensions

Tags:

This is my docker-compose file. Is there any easy way to get a postgres extension installed? I'm trying to install pg_trgm.

Edit: I now have two dockerfiles and an install script. It doesn't seem to be working when I run docker-compose up build

Internal server error: pq: operator does not exist: character varying % unknown

services:
  db:
    build:
      context: .
      dockerfile: db/Dockerfile
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=x
      - POSTGRES_PASSWORD=x
      - POSTGRES_DB=x

  api:
    build:
      context: .
      args:
        app_env: ${APP_ENV}
    volumes:
      - .:/go/src/x/y/z
    ports:
      - "8080:8080"

db/Dockerfile:

FROM postgres
COPY db/install-extensions.sql /docker-entrypoint-initdb.d

db/install-extensions.sql

CREATE EXTENSION IF NOT EXISTS pg_trgm;
like image 740
aroooo Avatar asked Apr 18 '19 08:04

aroooo


People also ask

How do I enable PostgreSQL extension?

To activate and use an extension, you must download and install the necessary files (if not delivered with the standard download) and issue the command CREATE EXTENSION <extension_name>; within an SQL client like psql . To control which extensions are already installed use: \dx within psql .

Should I run Postgres in docker?

Limitations of Running PostgreSQL Database with Docker While the quick start-up and easy configuration of Docker is a boon for development and testing, it's generally not advised to run production databases in Docker. The primary reason is that Docker containers are great for running stateless applications.


1 Answers

Try this

FROM postgres
COPY ./install-extensions.sql /docker-entrypoint-initdb.d

and remove db from your file.

OR you can write

version: "3.1"

services:
  db:
    image: postgres:9.6
    restart: always
    environment:
      POSTGRES_PASSWORD: unit
      POSTGRES_USER: unit
      POSTGRES_DB: unit
    ports:
      - 5432:5432
    volumes:
      - ./scripts:/docker-entrypoint-initdb.d

then

  1. create directory scripts
  2. put your .sql or .sh file
  3. remove created containers docker-compose rm -v
  4. start docker docker-compose up --build
  5. in logs you must see something like this:

created_extension

like image 150
Alma Z Avatar answered Sep 28 '22 01:09

Alma Z