summaryrefslogtreecommitdiff
path: root/temp-postgres.sh
diff options
context:
space:
mode:
Diffstat (limited to 'temp-postgres.sh')
-rwxr-xr-xtemp-postgres.sh31
1 files changed, 21 insertions, 10 deletions
diff --git a/temp-postgres.sh b/temp-postgres.sh
index 6064b66..8504257 100755
--- a/temp-postgres.sh
+++ b/temp-postgres.sh
@@ -20,8 +20,7 @@ set -o nounset
trap 'quit' INT
quit() {
code="${1:-0}"
- trap '' INT TERM
- kill -TERM 0
+ [ -z "$postgres_pid" ] || kill -TERM "$postgres_pid"
wait
rm -rf "${tmpdir-}" || {
( >&2 printf "temp-postgres: failed to remove temporary directory: %s\\n" "${tmpdir}" )
@@ -36,7 +35,7 @@ username="$( id -un )"
while [ $# -gt 0 ] ; do
case "$1" in
-h|--help)
- echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>]"
+ echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>] [--] [<command>]"
exit
;;
-d|--dbname)
@@ -60,10 +59,6 @@ while [ $# -gt 0 ] ; do
;;
esac
done
-if [ $# -gt 0 ] ; then
- ( >&2 echo "too many arguments" )
- exit 2
-fi
# Create a temporary directory
tmpdir="$( mktemp -d )" || {
@@ -79,6 +74,7 @@ initdb --pgdata="${tmpdir}" --username="${username}" || {
# Serve the directory
( postgres -k "${tmpdir}" -D "${tmpdir}" </dev/null ) &
+postgres_pid=$!
# Test the connection
sleep 1
@@ -96,8 +92,23 @@ createdb --host="${tmpdir}" --username="${username}" --no-password "${dbname}" |
printf '
Connect with the following command:
-\tpsql --host=localhost "%s" "%s"
+\tpsql --host "%s" --dbname "%s" --username "%s"
+
+' "$tmpdir" "$dbname" "$username"
-' "${dbname}" "${username}"
+if [ $# -eq 0 ] ; then
+ wait
+else
+ export PGHOST="$tmpdir"
+ export PGDATABASE="$dbname"
+ export PGUSER="$username"
+ export DATABASE_URI="postgresql:${dbname}?host=${tmpdir}&user=${username}"
+ export DATABASE_URL="$DATABASE_URI"
-wait
+ "$@" || {
+ code=$?
+ ( >&2 echo "temp-postgres: command exited ${code}" )
+ quit "$code"
+ }
+ quit
+fi
Generated by cgit. See skreutz.com for my tech blog and contact information.