Skip to content

Proposal: Support multiple database #175

Closed
@H6LS1S

Description

@H6LS1S

Description:

Add support for creating multiple databases when starting a container. This would be convenient for docker-compose. Implementation in other container.

Example:

/docker-compose.yaml
version: '3.5'
services:
  database:
    image: 'mdillon/postgis'
    container_name: Database
    ports:
      - '5432:5432'
    environment:
      POSTGRES_DB: db1, db2, db3
      POSTGRES_USER: dev
      POSTGRES_PASSWORD: dev

Solutions:

Replace the variable POSTGRES_DB with the expression $(echo $POSTGRES_DB | tr ',' ' '). This should give us the desired result:

  • If the string contains a comma-separated listing, break it into loop arguments
  • If the line contains only the phrase, then do nothing
/initdb-postgis.sh
#!/bin/sh

set -e

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

# Create the 'template_postgis' template db
"${psql[@]}" <<- 'EOSQL'
CREATE DATABASE template_postgis IS_TEMPLATE true;
EOSQL

# Load PostGIS into both template_database and $POSTGRES_DB
for DB in template_postgis $(echo $POSTGRES_DB | tr ',' ' '); do
	echo "Loading PostGIS extensions into $DB"
	"${psql[@]}" --dbname="$DB" <<-'EOSQL'
		CREATE EXTENSION IF NOT EXISTS postgis;
		CREATE EXTENSION IF NOT EXISTS postgis_topology;
		CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
		CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;
EOSQL
done

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions