Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 441
terrywb Avatar asked Feb 15 '18 19:02

terrywb


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 .

What is create extension in PostgreSQL?

Description. CREATE EXTENSION loads a new extension into the current database. There must not be an extension of the same name already loaded. Loading an extension essentially amounts to running the extension's script file.


1 Answers

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
like image 168
terrywb Avatar answered Sep 24 '22 09:09

terrywb