diff --git a/Dockerfile b/Dockerfile index ee9c3d0..2af149a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,3 @@ -FROM postgres:9.6 +ARG VER=9 +FROM postgis/postgis:$VER COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ diff --git a/README.md b/README.md index 3db6ca3..1cba44d 100644 --- a/README.md +++ b/README.md @@ -15,39 +15,27 @@ mechanism. ## Usage -### By mounting a volume - -Clone the repository, mount its directory as a volume into -`/docker-entrypoint-initdb.d` and declare database names separated by commas in -`POSTGRES_MULTIPLE_DATABASES` environment variable as follows -(`docker-compose` syntax): +You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable +to the container: myapp-postgresql: - image: postgres:9.6.2 - volumes: - - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d + image: public.ecr.aws/theplant/postgresmd:13 environment: - POSTGRES_MULTIPLE_DATABASES=db1,db2 - POSTGRES_USER=myapp - POSTGRES_PASSWORD= -### By building a custom image +## Push image -Clone the repository, build and push the image to your Docker repository, -for example for Google Private Repository do the following: - - docker build --tag=eu.gcr.io/your-project/postgres-multi-db . - gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db - -You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable -to the container: +Please assume AWS IAM `developer` role before push image +``` +[alias.main-developer] +arn = "arn:aws:iam::562055475000:role/developer" +``` - myapp-postgresql: - image: eu.gcr.io/your-project/postgres-multi-db - environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 - - POSTGRES_USER=myapp - - POSTGRES_PASSWORD= +``` +aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/theplant +``` ### Non-standard database names diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index aa665fa..ff6412f 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -13,10 +13,27 @@ function create_user_and_database() { EOSQL } +function create_extension_on_database() { + local database=$1 + local extension=$2 + echo " Creating extension '$extension'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d $database <<-EOSQL + CREATE EXTENSION IF NOT EXISTS $extension; +EOSQL +} + if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do create_user_and_database $db + + if [ ! -z "${POSTGRES_EXTENSIONS-}" ]; then + echo " Extensions creation requested: $db" + for ext in $(echo $POSTGRES_EXTENSIONS | tr ',' ' '); do + create_extension_on_database $db $ext + done + echo " Extensions created" + fi done echo "Multiple databases created" fi diff --git a/push_docker.sh b/push_docker.sh new file mode 100755 index 0000000..a44c994 --- /dev/null +++ b/push_docker.sh @@ -0,0 +1,17 @@ +docker build -t public.ecr.aws/theplant/postgresmd:13 --build-arg VER=13-3.5 . +docker push public.ecr.aws/theplant/postgresmd:13 + +docker build -t public.ecr.aws/theplant/postgresmd:14 --build-arg VER=14-3.5 . +docker push public.ecr.aws/theplant/postgresmd:14 + +docker build -t public.ecr.aws/theplant/postgresmd:15 --build-arg VER=15-3.5 . +docker push public.ecr.aws/theplant/postgresmd:15 + +docker build -t public.ecr.aws/theplant/postgresmd:16 --build-arg VER=16-3.5 . +docker push public.ecr.aws/theplant/postgresmd:16 + +docker build -t public.ecr.aws/theplant/postgresmd:17 --build-arg VER=17-3.5 . +docker push public.ecr.aws/theplant/postgresmd:17 + +docker build -t public.ecr.aws/theplant/postgresmd:18 --build-arg VER=18-3.6 . +docker push public.ecr.aws/theplant/postgresmd:18