From c60218136df5c07a533ff71b880cbe2503ac946d Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:00:26 -0400 Subject: [PATCH 1/6] Update create-multiple-postgresql-databases.sh quoted database names in script to fix special chars in database names --- create-multiple-postgresql-databases.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index aa665fa..fa2a3f5 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -4,19 +4,20 @@ set -e set -u function create_user_and_database() { - local database=$1 - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $database; + local database=$1 + echo " Creating user and database '$database'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER "$database"; + CREATE DATABASE "$database"; + GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$database"; + GRANT ALL PRIVILEGES ON DATABASE "$database" TO $POSTGRES_USER; 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 - done - echo "Multiple databases created" + echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" + for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do + create_user_and_database $db + done + echo "Multiple databases created" fi From c0d84d8e3dee87003115c171b719e0eb10f0f44c Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:02:18 -0400 Subject: [PATCH 2/6] Update README.md --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d4b6a15..2d748cf 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ Clone the repository, mount its directory as a volume into volumes: - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: - - POSTGRES_MULTIPLE_DATABASES: db1,db2 - - POSTGRES_USER: myapp - - POSTGRES_PASSWORD: + - POSTGRES_MULTIPLE_DATABASES=db1,db2 + - POSTGRES_USER=myapp + - POSTGRES_PASSWORD=mypassword ### By building a custom image @@ -45,13 +45,14 @@ to the container: myapp-postgresql: image: eu.gcr.io/your-project/postgres-multi-db environment: - - POSTGRES_MULTIPLE_DATABASES: db1,db2 - - POSTGRES_USER: myapp - - POSTGRES_PASSWORD: + - POSTGRES_MULTIPLE_DATABASES=db1,db2 + - POSTGRES_USER=myapp + - POSTGRES_PASSWORD=mypassword ### Non-standard database names -If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: +If you need to use non-standard database names (hyphens, uppercase letters etc), no quotes are needed with this fork: environment: - - POSTGRES_MULTIPLE_DATABASES: "test-db-1","test-db-2" + - POSTGRES_MULTIPLE_DATABASES=radarr-main,radarr-log,sonarr-main,sonarr-log,bazarr,readarr-audiobook-main,readarr-audiobook-log,readarr-audiobook-cache,readarr-ebook-main,readarr-ebook-log,readarr-ebook-cache,prowlarr-main,prowlarr-log,jellyseerr + From 9109f6757d5cf35eecc058468d6fc0afbf5d3dd5 Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:10:11 -0400 Subject: [PATCH 3/6] Create docker-image.yml github action to auto-build container --- .github/workflows/docker-image.yml | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..5e6114a --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,43 @@ +name: Create and publish a Docker image + +on: + workflow_dispatch: + push: + branches: [ "latest" ] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From b5bfb443b53708e686a8aec06e58edc8d4d5ab07 Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:12:51 -0400 Subject: [PATCH 4/6] Update Dockerfile Change from postgres 9.6 to postgres 14 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ee9c3d0..6165046 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,2 @@ -FROM postgres:9.6 +FROM postgres:14 COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ From fb75462535f249454963faa070d1cc4826d38c1e Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:16:17 -0400 Subject: [PATCH 5/6] Update Dockerfile --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 6165046..7f75bf5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,4 @@ FROM postgres:14 COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ + +LABEL org.opencontainers.image.source="https://github.com/captmicr0/docker-postgresql-multiple-databases" From 27f2c84cc151e010ad34b574c19cc6080b04cfb4 Mon Sep 17 00:00:00 2001 From: captmicr0 <92401273+captmicr0@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:18:50 -0400 Subject: [PATCH 6/6] Update README.md --- README.md | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 2d748cf..bd44a78 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,10 @@ entrypoint calls `initdb` to create the default `postgres` user and database, it will run any `*.sql` files and source any `*.sh` scripts found in that directory to do further initialization before starting the service.* -This directory contains a script to create multiple databases using that -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): +## Usage (docker-compose) myapp-postgresql: - image: postgres:9.6.2 + image: ghcr.io/captmicr0/docker-postgresql-multiple-databases:latest volumes: - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: @@ -31,24 +21,6 @@ Clone the repository, mount its directory as a volume into - POSTGRES_USER=myapp - POSTGRES_PASSWORD=mypassword -### By building a custom 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: - - myapp-postgresql: - image: eu.gcr.io/your-project/postgres-multi-db - environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 - - POSTGRES_USER=myapp - - POSTGRES_PASSWORD=mypassword - ### Non-standard database names If you need to use non-standard database names (hyphens, uppercase letters etc), no quotes are needed with this fork: