Created
April 24, 2019 13:35
-
-
Save RyanHirsch/c45ed23da77a713525eb334f223a9356 to your computer and use it in GitHub Desktop.
docker-compose postgres initialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Expected to be in a scripts directory | |
| set -e | |
| DATABASE_NAME="your-database" | |
| SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
| PROJECT_DIR="$( cd "${SCRIPTS_DIR}" && cd .. && pwd )" | |
| echo "==== Start database and running all migration and seed scripts ====" | |
| cd "${PROJECT_DIR}" | |
| docker-compose up -d | |
| echo "" | |
| echo "" | |
| echo "==== Waiting for database to spin up ====" | |
| docker-compose exec db bash -c "until pg_isready -U postgres; do sleep 2; done;" | |
| echo "" | |
| echo "" | |
| echo "==== Initializing Database ====" | |
| docker-compose exec db bash -c "psql -U postgres -tc \"SELECT 1 FROM pg_database WHERE datname = '${DATABASE_NAME}'\" | grep -q 1 || psql -U postgres -c \"CREATE DATABASE ${DATABASE_NAME}\"" | |
| echo "" | |
| echo "" | |
| echo "==== Running Database Migrations ====" | |
| yarn db:migrate | |
| echo "" | |
| echo "" | |
| echo "==== Running Database Migrations ====" | |
| yarn db:seed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version: "3" | |
| services: | |
| db: | |
| image: postgres | |
| restart: always | |
| environment: | |
| POSTGRES_PASSWORD: secretPassword | |
| PGDATA: /data/postgres | |
| ports: | |
| - 5432:5432 | |
| volumes: | |
| - database_data:/data/postgres | |
| pgadmin: | |
| image: dpage/pgadmin4 | |
| environment: | |
| PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]} | |
| PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin} | |
| volumes: | |
| - pgadmin:/root/.pgadmin | |
| ports: | |
| - "${PGADMIN_PORT:-5050}:80" | |
| restart: unless-stopped | |
| volumes: | |
| database_data: | |
| pgadmin: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment