Skip to content

Instantly share code, notes, and snippets.

@bradbergeron-us
Last active August 29, 2015 14:22
Show Gist options
  • Save bradbergeron-us/5d508396d58b2ac442b3 to your computer and use it in GitHub Desktop.
Save bradbergeron-us/5d508396d58b2ac442b3 to your computer and use it in GitHub Desktop.
Use for importing a database -> Add to init shell script then drop into ***/docker-entrypoint-initdb.d *** Start Postgres import via "pg_restore" then "stop database"
#!/bin/bash
: ${DB_USER:=db_user}
: ${DB_PASSWORD:=db_pass}
: ${DB_NAME:=db_name}
: ${DB_ENCODING:=UTF-8}
: ${DB_PG_DUMP_FILE:=/tmp/db.pgdump}
{ gosu postgres postgres --single -jE <<-EOSQL
CREATE USER "$DB_USER" WITH PASSWORD '$DB_PASSWORD';
EOSQL
} && { gosu postgres postgres --single -jE <<-EOSQL
CREATE DATABASE "$DB_NAME" WITH OWNER="$DB_USER" TEMPLATE=template0 ENCODING='$DB_ENCODING';
EOSQL
} && { gosu postgres pg_ctl start -w && gosu postgres pg_restore -d "$DB_NAME" "$DB_PG_DUMP_FILE" && gosu postgres pg_ctl stop -w
} && /bin/rm -f ${DB_PG_DUMP_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment