diff --git a/README.md b/README.md index 2d1f3ce..502a8f6 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,10 @@ These are simple steps using a single container. ### Manually create needed postgres databases -This is still necessary after `docker-compose up` to avoid +docker-compose now runs a db init script, but this information is useful for +debugging/manual work. + +~~This is still necessary after `docker-compose up` to avoid~~ [error] failed to initialize database, got error failed to connect to `user=exampleuser database=server_db`: 172.25.0.3:5432 (postgres): server @@ -122,7 +125,7 @@ The steps: # List all dbs \l -To run pgmin, the minimal go/postgres test part of this repository: +### To run pgmin, the minimal go/postgres test part of this repository 1. Run pgmin diff --git a/docker-compose.yml b/docker-compose.yml index 3620c1a..55957eb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,8 @@ services: POSTGRES_PASSWORD: examplepass POSTGRES_DB: exampledb volumes: - - postgres_data:/Users/hohn/var/lib/postgresql/data + - postgres_data:/var/lib/postgresql/data + - ./postgres-init-scripts:/docker-entrypoint-initdb.d ports: - "5432:5432" # Exposing PostgreSQL to the host expose: @@ -38,7 +39,7 @@ services: container_name: server command: sh -c "apt-get update && apt-get install -y curl && tail -f /dev/null" ports: - - "8080:80" # Exposing port 80 inside the container as port 8080 on the host + - "8080:8080" volumes: - /Users/hohn/work-gh/mrva/mrvacommander:/mrva/mrvacommander depends_on: diff --git a/postgres-init-scripts/dbinit.sh b/postgres-init-scripts/dbinit.sh new file mode 100755 index 0000000..37b058d --- /dev/null +++ b/postgres-init-scripts/dbinit.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + SELECT 'CREATE DATABASE server_db' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'server_db')\gexec + SELECT 'CREATE DATABASE querypack_db' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'querypack_db')\gexec + SELECT 'CREATE DATABASE qldb_db' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'qldb_db')\gexec +EOSQL