postgresqldockercodenvy

Create postgres extension in docker container


I am attempting to create a postgres instance in a Docker container using the following Dockerfile.

FROM postgres

ENV POSTGRES_DB dspace
ENV POSTGRES_USER dspace
ENV POSTGRES_PASSWORD dspace

COPY init.sql /docker-entrypoint-initdb.d/

Here is my init.sql

create extension pgcrpyto

I am using the Codenvy service to run this container. When it initializes, I am seeing the following error.

[STDOUT] server started
[STDOUT] Reading package lists...
[STDOUT] Reading package lists...
[STDOUT] Building dependency tree...
[STDOUT] CREATE DATABASE
[STDOUT] 
[STDOUT] CREATE ROLE
[STDOUT] 
[STDOUT] 
[STDOUT] /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
[STDOUT] Reading state information...
[STDOUT] 2018-02-15 19:17:34.931 UTC [399] ERROR:  could not open extension control file "/usr/share/postgresql/10/extension/pgcrpyto.control": No such file or directory
[STDOUT] 2018-02-15 19:17:34.931 UTC [399] STATEMENT:  create extension pgcrpyto
[STDERR] psql:/docker-entrypoint-initdb.d/init.sql:1: ERROR:  could not open extension control file "/usr/share/postgresql/10/extension/pgcrpyto.control": No such file or directory

Note, I based my solution on the following post. How to create postgres extension inside the container?


Solution

  • The following repo has a solution to my question. It appears that I need to provide a shell script to execute the SQL.

    https://github.com/DSpace-Labs/DSpace-codenvy

    Dockerfile

    # From https://github.com/DSpace-Labs/dspace-dev-docker/tree/master/postgres
    FROM postgres
    
    ENV POSTGRES_DB dspace
    ENV POSTGRES_USER dspace
    ENV POSTGRES_PASSWORD dspace
    
    COPY install-pgcrypto.sh /docker-entrypoint-initdb.d/
    

    install-pgcrypto.sh

    #!/bin/bash
    set -e
    
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    create extension pgcrypto;
    EOSQL