Skip to content

Instantly share code, notes, and snippets.

@briceburg
Last active February 14, 2023 00:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briceburg/2d401427c97ded0f408a918a8cac10b2 to your computer and use it in GitHub Desktop.
Save briceburg/2d401427c97ded0f408a918a8cac10b2 to your computer and use it in GitHub Desktop.
official PostgresSQL docker images - create multiple databases
#!/bin/bash
set -e
if [ -n "$POSTGRES_DATABASES" ]; then
echo "POSTGRES_DATABASES provided. Creating multiple databases..." >&2
IFS=', '; for db in $POSTGRES_DATABASES; do
echo "Creating '$db'" >&2
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-ESQL
CREATE USER "$db";
CREATE DATABASE "$db";
GRANT ALL PRIVILEGES ON DATABASE "$db" TO "$db";
ESQL
done
fi
---
version: '3.7'
services:
postgres:
image: postgres:11.7
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASES: database-larry, database_curly, moe
volumes:
- create-postgresql-databases.sh:/docker-entrypoint-initdb.d/create-postgresql-databases.sh:ro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment