Skip to content

Instantly share code, notes, and snippets.

@alinz
Last active June 15, 2021 14:47
Show Gist options
  • Save alinz/3084ac9fb81ffd38c789ccaff4a5bed2 to your computer and use it in GitHub Desktop.
Save alinz/3084ac9fb81ffd38c789ccaff4a5bed2 to your computer and use it in GitHub Desktop.
Postgress in Docker
#!/bin/bash
# Usage: bash build.sh project latest .
PROJECT_NAME=$1
VERSION=${2:-latest}
DOCKERFILE_PATH=$3
build_docker_db () {
docker rmi "$PROJECT_NAME/db:$VERSION"
docker build -t "$PROJECT_NAME/db:$VERSION" $DOCKERFILE_PATH
docker rmi postgres:10.0-alpine
}
#!/bin/bash
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;
EOSQL
}
if [ $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"
fi
version: "3"
services:
db:
image: project/db:latest
environment:
- POSTGRES_MULTIPLE_DATABASES=mydb1,mydb2
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ./storage/db:/var/lib/postgresql/data
- ./storage/migration:/migration
ports:
- "5432:5432"
FROM postgres:10.0-alpine
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
# COPY ./goose /bin/goose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment