Search code examples
postgresqldockerdocker-composedockerfile

How to install PostgreSQL extension with Docker


I have Dockerfile:

FROM postgres:14.0

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y \
        build-essential \
        zlib1g-dev \       
        pkg-config \
        git \
        mc \
    && echo "2" | apt-get install -y postgresql-server-dev-all \
    && rm -rf /var/lib/apt/lists/*

RUN git clone https://github.com/pramsey/pgsql-gzip.git \
    && cd pgsql-gzip \
    && make \
    && make install

I use this file with docker-compose.yml and docker-compose up -d command. I get access to the database, but the CREATE EXTENSION gzip command gives:

SQL Error [58P01]: ERROR: could not open extension control file "/usr/share/postgresql/14/extension/gzip.control": No such file or directory

How can I add the specified extension for Postgresql using Docker?


Solution

  • You are missing the clang-11 package to install, I added this package and postgresql-server-dev-14 also:

    Dockerfile_gzip

    FROM postgres:14.0
    
    ENV DEBIAN_FRONTEND=noninteractive
    
    RUN apt-get update && \
        apt-get install -y \
            build-essential \
            zlib1g-dev \       
            pkg-config \
            git \
            mc \
            postgresql-server-dev-14 \ 
            clang-11
    
    RUN git clone https://github.com/pramsey/pgsql-gzip.git \
        && cd pgsql-gzip \
        && make \
        && make install
    
    

    and after docker-compose up -d commad using this docker-compose file:

    version: "3"
    
    services:
        pg14:
            build: 
                context: .
                dockerfile: Dockerfile_gzip
            container_name: pg14-gzip
            ports:
                - 15566:5432
            env_file: pgsecrets.env
    

    pgsecrets.env file

    POSTGRES_PASSWORD=postgres
    POSTGRES_USER=postgres
    POSTGRES_INITDB_ARGS="-A trust"
    

    I was able to create the extension

    docker exec -it pg14-gzip bash
    root@98ceed5151c0:/# su - postgres
    postgres@98ceed5151c0:~$ psql
    psql (14.0 (Debian 14.0-1.pgdg110+1))
    Type "help" for help.
    
    postgres=# create extension gzip ;
    CREATE EXTENSION
    postgres=# \dx
                     List of installed extensions
      Name   | Version |   Schema   |         Description          
    ---------+---------+------------+------------------------------
     gzip    | 1.0     | public     | gzip and gunzip functions.
     plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
    (2 rows)
    
    postgres=#