Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env bash
set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior.
DATADIR=$(realpath "$1")
CONTAINER_NAME="postgres-${DATADIR//[^a-zA-Z0-9_.-]/_}"
IMAGE='postgres:latest'
COMMAND=''
if [ -n "${2-}" ]; then
COMMAND="-c \"$2\""
fi
if [ -t 0 ]; then
# used interactively
INTERACTIVE="-it"
else
# got stdin
INTERACTIVE="-i"
fi
POSTGRES_OPTS="-c 'max_connections=200' -c 'shared_buffers=4GB' -c 'effective_cache_size=12GB' -c 'maintenance_work_mem=1GB' -c 'checkpoint_completion_target=0.7' -c 'wal_buffers=16MB' -c 'default_statistics_target=100' -c 'random_page_cost=1.1' -c 'effective_io_concurrency=200' -c 'work_mem=1GB' -c 'min_wal_size=1GB' -c 'max_wal_size=2GB' -c 'max_worker_processes=8' -c 'max_parallel_workers_per_gather=8' -c 'max_parallel_workers=8'"
mkdir -p "$DATADIR"
docker run --rm \
"$INTERACTIVE" \
--name "$CONTAINER_NAME" \
-v "$DATADIR":/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=mysecretpassword \
--user $UID \
--shm-size=8g \
"$IMAGE" \
bash -c "(./docker-entrypoint.sh postgres $POSTGRES_OPTS &) &>/dev/null; until pg_isready -U postgres &>/dev/null; do sleep 0.1; done; psql -U postgres -d postgres --echo-all -v ON_ERROR_STOP=1 $COMMAND; EXIT_CODE=\$?; pg_ctl stop &>/dev/null; exit \$EXIT_CODE"